最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

exiting setInterval method in javascript - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jul 6, 2013 at 18:33 nicosantangelo 13.7k3 gold badges34 silver badges47 bronze badges asked Jul 6, 2013 at 18:31 Udanesh NUdanesh N 1712 gold badges2 silver badges15 bronze badges 7
  • 4 possible duplicate of Stop setInterval call in javascript – JJJ Commented Jul 6, 2013 at 18:32
  • @juhana is it the same?? i want to exit the setInterval() from inside ,on executing the if condition unless it should continue – Udanesh N Commented Jul 6, 2013 at 18:36
  • Yes, it is the same. Unless you're talking about exiting the function in that particular case, when you can just use return. – JJJ Commented Jul 6, 2013 at 18:36
  • You mean i have to call the clearInterval() from inside the if is it? – Udanesh N Commented Jul 6, 2013 at 18:40
  • Yes, just like Nico's answer shows. – JJJ Commented Jul 6, 2013 at 18:40
 |  Show 2 more comments

1 Answer 1

Reset to default 23
    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.

发布评论

评论列表(0)

  1. 暂无评论