在javascript中退出setInterval方法

时间:2013-07-06 18:31:43

标签: javascript

我有一个setInterval()函数,使用如下

        setInterval(function(){

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

        },1000);
或者告诉我 退出的方法是什么。(在java中我们使用System.exit(0))

1 个答案:

答案 0 :(得分:21)

    var timerId = setInterval(function(){

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

    },1000);

如果if它不是函数中的最后一件事而你想“破坏”执行,也许你想在return;之后添加clearInterval语句。 / p>