我想在删除包含 setInterval
函数的 div 后自动执行 clearInterval
函数,这个 div 是 ajax 响应的结果,因为它在删除后不会自行停止分区
$('#element').mouseover(function(){
$.post('ajax/ajax.php', function(data) {
$('<div id="tooltip'></div>").appendTo("div#main");
$('#tooltip').html(data);
$('#tooltip').show();
});
}).mouseout(function(){
clearInterval(intervalId);
$('#tooltip').empty();
$('#tooltip').remove();
});
ajax 响应 data
包含带有间隔 Id 处理程序的 javascript setInterval
函数:
var intervalId = window.setInterval(pics_load, 3000);
我尝试使用 Jquery unload但同样的问题:
$('#tooltip').unload(function(){
clearInterval(intervalId);
}
我也尝试在 ajax 响应中使用它:
$(window).unload(function(){
clearInterval(intervalId);
}
最佳答案
您是否尝试过使用 $.data
将 intervalId 存储在 #element
本身?
$('#element').mouseover(function() {
var $this = $(this);
$.post('ajax/ajax.php', function(data) {
$('<div id="tooltip"></div>').appendTo("div#main");
$('#tooltip').html(data);
$('#tooltip').show();
// save interval id here
var intervalId = setInterval('pics_load', 3000);
$this.data('intervalId', intervalId);
});
}).mouseout(function(){
// retrieve intervalId here
var intervalId = $(this).data('intervalId');
clearInterval(intervalId);
$('#tooltip').empty();
$('#tooltip').remove();
});
关于javascript:删除元素后停止setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10671256/