我已经在单击的div数组上设置间隔。我需要一种方法来清除鼠标悬停在特定div上的时间间隔以暂停间隔,然后在不悬停在特定div上时返回设置的时间间隔。暂停在div上悬停的时间间隔
<script type="text/javascript">
var arr = ['#tog1', '#tog2', '#tog3', '#tog4', '#tog5', '#tog6', '#tog7', '#tog8', '#tog9'];
var index = 0;
setInterval(function() {
$(arr[index++]).trigger('click');
if (index == arr.length)
}, 4000);
</script>
所以,如果我悬停在一个div称为.compBox
它需要清除的时间间隔,当非盘旋返回到的setInterval。
**更新**
所以使用一些建议下面我现在在这里,所以这样看来,它承认我徘徊在.compBox
但是,这不是停止阵列上的间隔:
var arr = ['#tog1', '#tog2', '#tog3', '#tog4', '#tog5', '#tog6', '#tog7', '#tog8', '#tog9', '#tog10'];
var index = 0;
var int = setInterval(function() {
console.log("working")
$(arr[index++]).trigger('click');
if (index == arr.length)
index = 0
}, 4000);
function handleInterval() {
$('.compBox').hover(function() {
//When the mouse enters the container, clear the interval
clearInterval(int)
}, function() {
//When the mouse leaves the container, reset the interval
setInterval(function() {
console.log("working")
$(arr[index++]).trigger('click');
if (index == arr.length)
index = 0
}, 4000);
});
}
handleInterval();
你没有重置初始化int变量作为mouseleave上的间隔。出于您的目的,我建议使用@jcuenod解决方案,因为它不会处理太多的全局变量,就像我的做法一样。 –
@SeanKwon我正在努力使它与我有的代码一起工作,很努力去理解tia这个例子的哪些地方,你能提供建议吗? – PhpDude
检查这个小提琴https://jsfiddle.net/j2bdL8op/我用控制台替换你的触发器点击 –