我有 2 个 setInterval 函数(好吧,伙计们,对不起,我认为里面的代码可能是多余的,并且会使问题变得本地化:/但无论如何,这里是:
$('#armStatus').click(function(){
armingloop = setInterval(function () {
if ($checkbox.is(':checked ')) {
$.post('/request', {
key_pressed: "arming_status"
}).done(function (reply) {
$arm.empty().append("<h3>The Arming Status is " + reply + "</h3>").show();
$arm.show();
});
} else {
$arm.hide();
}
}, 3000);
});
和
$('#monitor').click(function () {
bigloop = setInterval(function () {
var checked = $('#status_table tr [id^="monitor_"]:checked');
if (checked.index() === -1 || checked.length === 0) {
clearloop(bigloop);
$('#monitor').button('enable');
} else {
//$('#monitor').button('enable'); //enable the monitor button
(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
});
function clearloop(loopname){
bigloop= window.clearInterval(loopname);
}
两者都将由不同的选择器触发。我观察到,当bigloop
被激活,并且在以后也被激活时,我armingloop
的状态更新功能受到了影响(例如状态回复被错误的元素捕获。)monitoring
bigloop
请注意,我也有一个setTimer
。
我的问题是,我怎样才能确保任何 2 setInterval
s 被隔离并且不会相互影响?