92
myInterval = setInterval(function(){
     MyFunction();
},50);

function MyFunction()
{
    //Can I call clearInterval(myInterval); in here?
}

The interval's not stopping (not being cleared), if what I've coded above is fine then it'll help me look elsewhere for what's causing the problem. Thanks.

EDIT: Let's assume it completes a few intervals before clearInterval is called which removes the need for setTimeout.

5

5 Answers 5

162

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);
};
4
  • 2
    How can myInterval be passed to foo when the setInterval hasn't been evaluated yet?
    – Melab
    Commented Apr 30, 2019 at 2:22
  • 1
    @Melab which of the three code block examples are you referring to?
    – jbabey
    Commented May 1, 2019 at 13:15
  • Just to note, testing this using let instead of var doesn't seem to work in a react app
    – elfico
    Commented Feb 13, 2020 at 10:37
  • @Melab the function inside setInterval is not executing on creation but called by setInterval, meaning that myInterval would already be evaluated Commented Aug 23, 2022 at 15:31
11
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.

2
  • 1
    But can this be called within the interval itself? i.e. can it stop itself?
    – user1017882
    Commented Aug 23, 2012 at 13:18
  • Yep, no probs, I removed my comment.
    – user1017882
    Commented Aug 23, 2012 at 13:19
3
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);
1
  • 1
    I'm getting a few different answers here, and yours seems to be the ideal one as I'm needing to cancel it 'within' the interval itself. Going by the first part of your answer what I've done in the OP is ok, so I'm going to assume there's no problem there.
    – user1017882
    Commented Aug 23, 2012 at 13:21
3

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 :)

3
  • If you down vote, please comment on what was wrong/badly done, thank you :)
    – jave.web
    Commented Mar 30, 2015 at 9:29
  • I know SO is known for "How to do A?" "You don't do A, you do B" but seriously, this is actually the preferred way. Commented Mar 2, 2023 at 17:10
  • @MImamPratama Although I agree with that for most cases, I wouldn't force that, sometimes you don't care about sequence, but just a regular trigger - in that time setInterval could (still doesn't have to) be a better option
    – jave.web
    Commented Mar 3, 2023 at 13:48
2

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.