How can I use the clearInterval the function when function meets some condition?
Code:-
var checkRecordlock = function() {
jQuery.ajax({
url: "http://localhost/project/crl/Mzk="
}).done(function(data) {
var is_locked = data.locked;
if (is_locked == 1) {
alert("Locked");
}
});
}
checkRecordlock();
var checkRecordlockIntervalId = setInterval(checkRecordlock, 100);
I want to clear the interval of the function if is_locked == 1
.
2
Answers
You don’t need to call
checkRecordlock
before the interval. In order to stop it when the condition reaches, useclearInterval
and pass itcheckRecordlockIntervalId
. Here is an example: