JavaScript 中 0 毫秒的 setInterval() 行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7648557/
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
setInterval() behaviour with 0 milliseconds in JavaScript
提问by Jo?o Gomes
In my application I found some JavaScript code that is using setInterval
with 0 milliseconds, like so:
在我的应用程序中,我发现了一些使用setInterval
0 毫秒的JavaScript 代码,如下所示:
self.setInterval("myFunction()",0);
Obviously, this does not seem like a good idea to me. Can anyone tell me what will be the behaviour of setInterval
here? ("myFunction" makes an AJAX call to the server)
显然,这对我来说似乎不是一个好主意。谁能告诉我setInterval
这里的行为是什么?(“myFunction”对服务器进行 AJAX 调用)
I am asking this because I am having an irregular behaviour in my application. 90% of the times, the application behaves correctly and exactly one call to the server is made. However sometimes, multiple calls are made to the server (until now, maximum is 48 calls) and I am almost certain it is the fault of this line of code.
我问这个是因为我在我的申请中有不正常的行为。90% 的情况下,应用程序运行正常,并且只调用一次服务器。但是,有时会向服务器发出多次调用(到目前为止,最多调用 48 次),我几乎可以肯定这是这行代码的错误。
回答by deadalnix
Browser set a minimal value for the interval. Usualy 10ms, but it can depend on the browser. This means repeat this as fast as I'm possibly allowed. The W3C spec say 4ms : http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers
浏览器为间隔设置一个最小值。通常为 10 毫秒,但它可能取决于浏览器。这意味着尽可能快地重复这个。W3C 规范说 4 毫秒:http: //www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers
This is correct but probably reveal a design error.
这是正确的,但可能揭示了设计错误。
EDIT: By the way, it is bad practice to pass a string to setTimeout/setInterval, pass a function instead as javascript has first class functions.
编辑:顺便说一下,将字符串传递给 setTimeout/setInterval 是不好的做法,而是传递一个函数,因为 javascript 具有一流的函数。
回答by Narendra Yadala
setInterval(myFunction, 0)
calls myFunction
continuously with minimum delay. It is almost like calling myFunction
in a infinite loop. Except that here you can stop the loop using the clearInterval
method.
setInterval(myFunction, 0)
myFunction
以最小的延迟连续呼叫。这几乎就像myFunction
在无限循环中调用。除了在这里您可以使用该clearInterval
方法停止循环。
回答by Shadow Wizard is Ear For You
To have it executed only once with minor delay, use setTimeOut
instead:
要使其仅以轻微延迟执行一次,请setTimeOut
改用:
window.setTimeout(myFunction, 10);
As you're using AJAX, you don't have to use any timers at all - just call the next AJAX request in the Callback (complete/success event) of the current AJAX request.
当您使用 AJAX 时,您根本不必使用任何计时器 - 只需在当前 AJAX 请求的回调(完成/成功事件)中调用下一个 AJAX 请求。
Post your current code and we might be able to guide you further.
发布您当前的代码,我们或许可以为您提供进一步的指导。
回答by Brad
I assume that in myFunction()
there is a clearInterval
.
我假设myFunction()
里面有一个clearInterval
.
Basically, you've set an interval that can happen as often as possible. If the browser executing JavaScript actually gets to the clearInterval
part before the next iteration of the interval, then it will be fine. Otherwise, it will happen again and again.
基本上,您已经设置了一个尽可能经常发生的间隔。如果执行 JavaScript 的浏览器clearInterval
在下一次间隔迭代之前真正到达了那部分,那就没问题了。否则,它会一次又一次地发生。
Use setTimeout
instead.
使用setTimeout
来代替。