0

我怎样才能让这个动画停在最后一张图片上,让它站在最后一张图片上?也许你可以帮我停止这个动画。

var myImages = [
    "http://placekitten.com/200/200",
    "http://placekitten.com/150/150",
    "http://placekitten.com/180/180",
    "http://placekitten.com/170/170",
    "http://placekitten.com/140/150",
    "http://placekitten.com/160/160"
];
var counter = 1;  // Start at number 2 since the HTML tag has the first

function switchImage() {
    $('#myImage').attr('src', myImages[counter]);
    counter += 1;

    if (counter == myImages.length) {
        counter = 0;
    }
}

$(document).ready(function() {
    setInterval(switchImage, 5000);
});
4

1 回答 1

0

setInterval()返回一个间隔 ID,您可以将其传递给clearInterval().

var refreshIntervalId;
var myImages = [
    "http://placekitten.com/200/200",
    "http://placekitten.com/150/150",
    "http://placekitten.com/180/180",
    "http://placekitten.com/170/170",
    "http://placekitten.com/140/150",
    "http://placekitten.com/160/160"
];
var counter = 1;  // Start at number 2 since the HTML tag has the first

function switchImage() {
    $('#myImage').attr('src', myImages[counter]);
    counter += 1;

    if (counter == myImages.length) {
        counter = 0;
        clearInterval(refreshIntervalId);
    }
}

$(document).ready(function() {
   refreshIntervalId = setInterval(switchImage, 5000);
});

DEMO FIDDLE

于 2013-10-06T19:29:25.137 回答