我有以下代码:运行在超时循环功能
// After 8 seconds change the slide...
var timeout = setTimeout("changeSlide()", 8000);
$('div.slideshow').mouseover(function() {
// If the user hovers the slideshow then reset the setTimeout
clearTimeout(timeout);
});
$('div.slideshow').mouseleave(function() {
clearTimeout(timeout);
var timeout = setTimeout("changeSlide()", 8000);
});
我希望发生的是使函数changeSlide运行每8秒钟一个循环,除非你把光标移动幻灯片股利。当他们删除光标,然后再次超时!
然而循环只发生一次,悬停不会停止超时或再次启动它:/
编辑:
这个循环伟大的,但上落徘徊导致函数运行多个次:
// After 8 seconds change the slide...
var timeout = setInterval(changeSlide, 2000);
$('div.slide').mouseover(function() {
// If the user hovers the slideshow then reset the setTimeout
clearInterval(timeout);
});
$('div.slide').mouseleave(function() {
clearInterval(timeout);
var timeout = setInterval(changeSlide, 2000);
});
酷我已经根据你的答案改变了代码,但它仍然没有循环超时? – Cameron 2012-04-05 15:23:40
关键是在每个changeSlide结束时再次调用setTimeout,它将排队等待下一个更改。你也可以使用setInterval,重复使用,而不是一次 – Matt 2012-04-05 15:25:02