Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exiting setInterval method in javascript

Tags:

javascript

i have a setInterval() function which is used as follows

        setInterval(function(){

           if(window.document.drops.isFinished()){
               //I want to exit the setInterval() on executing this if
           }

        },1000);

or tell me what is the method to exit.(In java we use System.exit(0))

like image 552
Udanesh N Avatar asked Jul 06 '13 18:07

Udanesh N


1 Answers

    var timerId = setInterval(function(){

       if(window.document.drops.isFinished()){
           clearInterval(timerId);
       }

    },1000);

If the if it's not the last thing in the function and you want to "break" the execution, maybe you want to also add a return; statement after the clearInterval.

like image 120
nicosantangelo Avatar answered Oct 23 '22 08:10

nicosantangelo