我有以下代码:
// 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 秒循环运行一次,除非有人悬停在幻灯片 div 上。当他们删除光标然后再次超时!
然而,循环只发生一次,悬停不会停止超时或再次启动它:/
编辑:
这循环很好,但悬停打开和关闭会导致函数运行多次:
// 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);
});