Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript setInterval function to clear itself?

People also ask

Which function will stop the continuation of setInterval function?

The JavaScript setInterval() method returns an ID which can be used by the clearInterval() method to stop the interval.

Does setInterval wait for function to finish?

The setInterval method is used to schedule a function to be executed repeatedly after a period of time. The syntax for this method is this: const timerID = setInterval(function, delay, arg1, arg2, ...) In this case, the delay is the time in milliseconds that the timer should delay successive executions of the function.

Does clearInterval stop immediately?

I did make a fiddle to test it, and it turns out clearInterval stops all future execution, even those that have already be queued.

Does setInterval fire right away?

Method 1: Calling the function once before executing setInterval: The function can simply be invoked once before using the setInterval function. This will execute the function once immediately and then the setInterval() function can be set with the required callback.


As long as you have scope to the saved interval variable, you can cancel it from anywhere.

In an "child" scope:

var myInterval = setInterval(function(){
     clearInterval(myInterval);
},50);

In a "sibling" scope:

var myInterval = setInterval(function(){
     foo();
},50);

var foo = function () {
    clearInterval(myInterval);
};

You could even pass the interval if it would go out of scope:

var someScope = function () {
    var myInterval = setInterval(function(){
        foo(myInterval);
    },50);
};

var foo = function (myInterval) {
    clearInterval(myInterval);
};

clearInterval(myInterval);

will do the trick to cancel the Interval whenever you need it. If you want to immediately cancel after the first call, you should take setTimeout instead. And sure you can call it in the Interval function itself.

var myInterval = setInterval(function() {
  if (/* condition here */){
        clearInterval(myInterval);
   } 
}, 50);

see an EXAMPLE here.


var interval = setInterval(function() {
  if (condition) clearInterval(interval); // here interval is undefined, but when we call this function it will be defined in this context
}, 50);

Or

var callback = function() { if (condition) clearInterval(interval); }; // here interval is undefined, but when we call this function it will be defined in this context
var interval = setInterval(callback, 50);

You can do it by using a trick with window.setTimeout

var Interval = function () {
    if (condition) {
        //do Stuff
    }
    else {
        window.setTimeout(Interval, 20);
    };
};
window.setTimeout(Interval, 20);

From your code what seems you want to do is to run a function and run it again and again until some job is done...

That is actually a task for the setTimeout(), the approach is similar:

    var myFunction = function(){
      if( stopCondition ) doSomeStuff(); //(do some stuff and don't run it again)
        else setTimeout( myFunction, 50 );
    }
    myFunction(); //immediate first run 

Simple as that :)

Of course if you REALLY want to use setInterval for some reason, @jbabey's answer seems to be the best one :)