在hover时添加暂停到setInterval()?
嗨这里是我的jquery函数幻灯片与其他内置函数。 当我将鼠标hover在整体div上时,我一直试图让它暂停。 帮帮我!
$(function() { var timer = setInterval( showDiv, 5000); var counter = 2; function showDiv() { if (counter ==0) { counter++; return; } $('div','#slide-show-overall') .stop() .fadeOut('slow') .filter( function() { return this.id.match('picture-set-' + counter); }) .fadeIn('slow'); if (counter ==2) { slideWings();} //extra functions if (counter ==1) { resetWings();} //extra functions counter == 3? counter = 1 : counter++; } }); });
您希望使用clearInterval
删除hover时间间隔,然后在关闭hoverfunction中将其替换:
$('#slide-show-overall').hover(function(ev){ clearInterval(timer); }, function(ev){ timer = setInterval( showDiv, 5000); });
$('#slide-show-overall > div').hover( function () { clearInterval(timer) }, function () { timer = setInterval( showDiv, 5000); } );
为什么不将整个’setInterval’进程放入函数中? 这样你就可以通过名字来阻止它并通过调用函数来启动它。
// the function - set var up just in case // the timer isn't running yet var timer = null; function startSetInterval() { timer = setInterval( showDiv, 5000); } // start function on page load startSetInterval(); // hover behaviour $('#slide-show-overall').hover(function() { clearInterval(timer); },function() { startSetInterval(); });