Как убедиться, что два setInterval() не будут влиять друг на друга?Jquery

Программирование на jquery
Гость
Как убедиться, что два setInterval() не будут влиять друг на друга?

Сообщение Гость »


I have 2 setInterval function (okay guys, sorry, I thought the code inside may be redundant and will make the question become localized :/ but anyway, here it is:

Код: Выделить всё

$('#armStatus').click(function(){
armingloop = setInterval(function () {
if ($checkbox.is(':checked ')) {
$.post('/request', {
key_pressed: "arming_status"
}).done(function (reply) {
$arm.empty().append("The Arming Status is " + reply + "").show();
$arm.show();
});
} else {
$arm.hide();
}
}, 3000);
});
and

Код: Выделить всё

$('#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);
}
Both will be triggered by a different selector. I observe that when the is activated, and

Код: Выделить всё

armingloop
is also activated at a later time, the status update function

Код: Выделить всё

monitoring
in my is affected (e.g. status reply is captured by wrong element.)
Note that I have a

Код: Выделить всё

setTimer
as well.
My question here is, how can i make sure any 2

Код: Выделить всё

setInterval
s are isolated and will not affect each other?[/b]


Источник: https://stackoverflow.com/questions/165 ... each-other

Вернуться в «Jquery»