停止setInterval我想在error处理程序中停止此间隔重复运行。这有可能,如果是的话,怎么样?// example code
$(document).on('ready',function(){
setInterval(updateDiv,3000);});function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
// I want to stop it here
}
});}
3 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您需要setInterval在单击处理程序的范围内设置变量的返回值,然后clearInterval()像这样使用:
var interval = null;$(document).on('ready',function(){
interval = setInterval(updateDiv,3000);});function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
clearInterval(interval); // stop the interval
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
}
});}
临摹微笑
TA贡献1982条经验 获得超2个赞
使用变量并调用clearInterval以停止它。
var interval;$(document).on('ready',function()
interval = setInterval(updateDiv,3000);
});
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
// I want to stop it here
clearInterval(interval);
}
});
}
慕斯709654
TA贡献1840条经验 获得超5个赞
您必须将setInterval函数的返回值分配给变量
var interval;$(document).on('ready',function(){
interval = setInterval(updateDiv,3000);});然后clearInterval(interval)再次使用它来清除它。
添加回答
举报
0/150
提交
取消
