javascript 退出javascript中的setInterval方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17505883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exiting setInterval method in javascript
提问by Udanesh N
i have a setInterval() function which is used as follows
我有一个 setInterval() 函数,它的用法如下
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))
或者告诉我退出的方法是什么。(在java中我们使用System.exit(0))
回答by nicosantangelo
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
.
如果if
它不是函数中的最后一件事并且您想“中断”执行,也许您还想return;
在clearInterval
.