function slideshow(){
var timer = 0;
if(++index > 6) index = 1;
$(".banner").attr("src", images[index-1]);
$(".textfield").text(captions[index-1]);
$(".num").text(index);
timer = setTimeout(slideshow,3000);
}
function stopShow(){
clearTimeout(timer);
}
$("#show").click(function(){
slideshow();
});
$("#stopshow").click(function(){
stopShow();
});
我有两个按钮分别用于开始和停止幻灯片放映。在上面的代码部分,当我单击停止幻灯片放映按钮时,它类 stopShow() 函数,但 setTimeout 循环不会在后台停止
最佳答案
我认为这与var timer
的范围有关。尝试将声明移到 slideshow()
函数之外。即:
var timer;
function slideshow(){
timer = 0;
....
}
function stopShow(){
clearTimeout(timer);
}
....
关于javascript - 如何停止图片的幻灯片放映?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052584/