Javascript setInterval 函数可以自行清除吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12092439/
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
Javascript setInterval function to clear itself?
提问by jbabey
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.
编辑:让我们假设它在调用 clearInterval 之前完成了几个间隔,这消除了对 setTimeout 的需要。
回答by jbabey
As long as you have scope to the saved interval
variable, you can cancel it from anywhere.
只要您有保存interval
变量的范围,您就可以从任何地方取消它。
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);
};
回答by Christoph
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.
将在您需要时取消间隔。如果您想在第一次通话后立即取消,您应该setTimeout
改为。并且确保您可以在 Interval 函数本身中调用它。
var myInterval = setInterval(function() {
if (/* condition here */){
clearInterval(myInterval);
}
}, 50);
see an EXAMPLE here.
回答by Danil Speransky
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);
回答by jave.web
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:
这实际上是 的任务setTimeout()
,方法类似:
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 :)
当然,如果您出于某种原因真的想使用 setInterval,@jbabey 的答案似乎是最好的答案:)
回答by maniac
You can do it by using a trick with window.setTimeout
您可以通过使用 window.setTimeout 的技巧来做到这一点
var Interval = function () {
if (condition) {
//do Stuff
}
else {
window.setTimeout(Interval, 20);
};
};
window.setTimeout(Interval, 20);