I'm wondering if there's a way to make setInterval
clear itself.
Something like this:
setInterval(function () {
alert(1);
clearInterval(this);
}, 2000);
I want it to keep checking thing if it's finished it will stop.
Try this way:
var myInterval = setInterval(function () {
alert(1);
clearInterval(myInterval);
}, 2000);
Alternative way is using setTimeout()
if you know it is just one time call.
(function() {
var runs = 3;
var i = setInterval(function() {
console.log("Number " + runs);
--runs;
if (runs == 0) {
clearInterval(i);
}
}, 1000);
}());
jsfiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With