如何停止图像的幻灯片放映?

len*_*nes 3 html javascript jquery

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();
});
Run Code Online (Sandbox Code Playgroud)

我有两个按钮分别启动和停止幻灯片放映.在上面的代码部分中,当我单击停止幻灯片按钮时,它会对stopShow()函数进行分类,但是setTimeout循环不会在后台停止

Sha*_*rop 5

我认为这与范围有关var timer.尝试在slideshow()函数外部移动声明.IE:

var timer;

function slideshow(){
    timer = 0;
    ....
}

function stopShow(){
    clearTimeout(timer);
}
....  
Run Code Online (Sandbox Code Playgroud)