【发布时间】:2016-06-20 01:19:21
【问题描述】:
如果我将鼠标移出之前触发间隔功能的元素,则间隔“myInterval”不会停止。为什么不停止?
$(".postimagepic").mouseenter(function () {
var link = $("#blurpost").attr("src").split("/").pop().split(".", 1)[0];
var myInterval = setInterval(function () {
$.ajax({
url: 'asdf.php',
type: 'post',
data: {
'user': 'yy',
'topost': link
},
success: function () {
}
});
}, 1000);
}).mouseout(function () {
clearInterval(myInterval);
});
【问题讨论】:
-
myInterval是mouseenter回调函数的私有。在事件绑定代码上方声明变量。 代码:var myInterval; .... $(".postimagepic").mouseenter(function() { myInterval = setInterval....。另外,请使用hover。 -
Moritz,Web 开发中的两个关键工具是浏览器的 Web 控制台(如果您查看,它会向您显示错误)及其内置调试器。使用 F12 或 Ctrl+Shift+I 或 Cmd+Shift+I 打开浏览器的开发工具。
标签: jquery intervals mouseleave mouseout