bigloop=setInterval(function () {
var checked = $('#status_table tr [id^="monitor_"]:checked');
if (checked.index()===-1 ||checked.length===0 || ){
bigloop=clearInterval(bigloop);
$('#monitor').button('enable');
}else{
(function loop(i) {
//monitor element at index i
monitoring($(checked[i]).parents('tr'));
//delay of 3 seconds
setTimeout(function () {
//when incremented i is less than the number of rows, call loop for next index
if (++i < checked.length) loop(i);
}, 3000);
}(0)); //start with 0
}
}, index*3000); //loop period
我有上面的代码,有时有效,有时无效。我想知道 clearInterval 是否真的清除了计时器?? 因为有这个
monitor
按钮只有在处于 monitoring
功能时才会被禁用。当单击名为 clearInterval
的元素时,我会得到另一个 .outputRemove
。请参阅下面的代码:
//remove row entry in the table
$('#status_table').on('click', '.outputRemove', function () {
deleted= true;
bigloop= window.clearInterval(bigloop);
var thistr=$(this).closest('tr');
thistr.remove();
$('#monitor').button('enable');
$('#status_table tbody tr').find('td:first').text(function(index){
return ++index;
});
});
但它启用了一段时间后又被禁用了。
clearInterval
会从setInterval
函数中取出程序吗?
是的,你可以。您甚至可以测试一下:
var i = 0;
var timer = setInterval(function() {
console.log(++i);
if (i === 5) clearInterval(timer);
console.log('post-interval'); // interval will be cleared after completing this whole block
}, 200);
在此示例中,当
i
达到 5 时,此计时器会清除。
让我们检查一下:
let clearMyself = setInterval(() => {
console.log('Hello World')
clearInterval(clearMyself)
}, 5)
这会输出一个
Hello World
,证明你可以。