var myImage = document.getElementById("mainImage");
var imageArray = ["_images/overlook.jpg","_images/winery_sign.jpg","_images/lunch.jpg",
"_images/bigSur.jpg","_images/flag_photo.jpg","_images/mission_look.jpg"];
var imageIndex = 0;
function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
我試圖重構這個問題restarting a setInterval,但無法得到它的權利。任何幫助,將不勝感激! ***添加上下文**** 基本上,我有一堆圖像,循環並停止點擊它們。我想在再次單擊重新啓動循環...
var intervalHandle = setInterval(changeImage,5000);
//Basically I want a clearInterval on a click and then restart this changing image function it.
myImage.onclick = function(){
clearInterval(intervalHandle);
intervalHandle = setInterval(changeImage,5000);
};
什麼是不正確的呢?會發生什麼,而不是你所期望的? –
更多的上下文會有所幫助。爲什麼你在點擊事件中再次開始了間隔? 我想如果我刪除了在點擊事件中設置間隔的呼叫,這將工作。 –
不要在click處理程序中重新調用setInterval。這會告訴你確定它是否正常工作 –