悬停时清除间隔
有一个定制的滑块,我想在悬停时停止。我尝试清除并设置悬停间隔,但无法正常工作。仅当我第一次将鼠标悬停在其上时,它才会停止,然后如果我将鼠标移出并再次移入,它就不会停止。
这是我的代码:
var itvl = null;
itvl = window.setInterval(function(){scroll_()},3000);
function scroll_() {
if ($('#carousel_ul li').length > 1) {
$("#right_scroll").click();
}
}
$('#carousel_ul li').hover(function() {
window.clearInterval(itvl);
}, function() {
window.setInterval(function(){scroll_()},3000);
});
知道我做错了什么吗?
预先感谢
毛罗
have a custom made slider which I would like to stop on hover. I've tried to clear and set the interval on hover but doesn't work properly. It stops only the first time I hover on it then if I move the mouse out and in again it doesn't stop.
here's my code:
var itvl = null;
itvl = window.setInterval(function(){scroll_()},3000);
function scroll_() {
if ($('#carousel_ul li').length > 1) {
$("#right_scroll").click();
}
}
$('#carousel_ul li').hover(function() {
window.clearInterval(itvl);
}, function() {
window.setInterval(function(){scroll_()},3000);
});
Any idea what am I doing wrong?
Thanks in advance
Mauro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您设置悬停关闭的时间间隔时,您并不是在设置 itvl。 itvl 实际上是一个整数,充当区间的引用。因此,当您执行
window.setInterval(function(){scroll_()},3000);
而不将其引用为任何内容时,引用会发生变化。试试这个:
When you are setting the interval on the hover-off, you are not setting itvl. itvl is actually an integer that acts as a reference to the interval. So the reference changes when you do
window.setInterval(function(){scroll_()},3000);
without reffing it to anything.Try this instead:
您可以使用布尔变量而不是clearInterval:
You can use a boolean variable rather than clearInterval: