好的,所以我有一个非常简单的图像幻灯片,它在jsfiddle
正如你们所看到的,当您单击开始时它工作正常。但是,当您单击停止时,功能继续运行这里是 jquery:
$(document).ready(function() {
var timer;
$('img.hidden').hide();
$('img.top').show();
$('input').click(function(){
var value = $('input').attr("value");
if(value == "start"){
$(this).attr("value","stop");
startShow();
}else if(value == "stop"){
$(this).attr("value","start");
alert('stopped');
clearTimeout(timer);
}
});
});
function startShow(){
var $top = $('img.top').attr("src");
var $last = $('img').last().attr("src");
if($top != $last ){
var $topImg = $('img.top');
var $nextImg = $('img.top').next();
$topImg.hide();
$nextImg.show();
$topImg.removeClass("top");
$nextImg.addClass("top");
}
else{
var $topImg = $('img.top');
var $nextImg = $('img').first();
$topImg.hide();
$nextImg.show();
$topImg.removeClass("top");
$nextImg.addClass("top");
}
timer = setTimeout(function() { startShow(); }, 2000);
};