1

I tried to add the different images with fadeIn and fadeOut Option. Right Now that Script makes continuously looping. But i want to make that stop the loop once it ends the last of the image and alert. That's it. I am not sure how can i do this inorder to get alert for end of the loop.

$('document').ready(function() {
    var $imgs = $('#slideshow > img'), current = 0;

    var nextImage = function() {
        if (current >= $imgs.length) current = 0;
        $imgs.eq(current++).fadeIn(function() {
            $(this).delay(3000).fadeOut(nextImage);
        })
    };
nextImage();
});

Fiddle

4

1 回答 1

3

你需要更换:

if (current >= $imgs.length) current = 0;

和:

if (current >= $imgs.length) { 
    alert('end of images'); 
    return false; 
}

return false;将立即退出该方法。

最终代码应如下所示:

$('document').ready(function() {
    var $imgs = $('#slideshow > img'), current = 0;

    var nextImage = function() {
        if (current >= $imgs.length) {
            alert('End of images');
            return false;
        }
        $imgs.eq(current++).fadeIn(function() {
            $(this).delay(3000).fadeOut(nextImage);
        })
    };
    nextImage();
});

希望有帮助。

于 2013-10-21T08:59:17.107 回答