javascript setInterval

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5801543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:56:01  来源:igfitidea点击:

javascript setInterval

javascript

提问by Omega

a question. If i use setInterval in this manner:

一个问题。如果我以这种方式使用 setInterval:

setInterval('doSome();',60000);

am i safe that the doSome()function is triggered every 60 seconds, even if I change the tab in a browser?

doSome()即使我更改浏览器中的选项卡,该功能每 60 秒触发一次是否安全?

回答by Delan Azabani

Passing a string to setIntervalis fine, and is one of two ways to use setInterval, the other is passing a function pointer. It is not wrong in any way like the other answers state, but it is not as efficient (as the code must be reparsed) nor is it necessary for your purpose. Both

将字符串传递给setInterval很好,这是两种使用方法之一setInterval,另一种是传递函数指针。它在任何方面都没有错误,就像其他答案所述,但效率不高(因为必须重新解析代码),也不是您的目的所必需的。两个都

setInterval('doSome();', 60000); // this runs doSome from the global scope
                                 // in the global scope

and

setInterval(doSome, 60000);      // this runs doSome from the local scope
                                 // in the global scope

are correct, though they have a slightly different meaning. If doSomeis local to some non-global scope, calling the latter from within the same scope will run the local doSomeat 60000ms intervals. Calling the former code will always look for doSomein the global scope, and will fail if there is no doSomefunction in the global scope.

是正确的,尽管它们的含义略有不同。如果doSome是某个非全局范围的本地doSome,则从同一范围内调用后者将以60000 毫秒的间隔运行本地。调用前面的代码会一直doSome在全局范围内查找,如果doSome全局范围内没有函数就会失败。

The function will reliably be triggered, regardless of tab focus, at intervals of at least60000ms, but usually slightly more due to overheads and delays.

无论选项卡焦点如何,该函数都会以至少60000 毫秒的间隔可靠地触发,但由于开销和延迟,通常会稍微多一点。

All browsers clamp the interval valueto at least a certain value to avoid intervals being too frequent (I think it's a minimum of 10ms or 4ms or something, I can't exactly remember).

所有浏览器都将间隔值限制为至少某个值以避免间隔太频繁(我认为它至少是 10 毫秒或 4 毫秒之类的,我记不清了)。

Note that some browsers (the upcoming Firefox 5 is one, but there are probably others that I don't know of) further clamp setIntervaldrastically to e.g. 1000ms if the tab is not focused. (Reference)

请注意,setInterval如果选项卡未聚焦,某些浏览器(即将推出的 Firefox 5 就是其中之一,但可能还有其他我不知道的浏览器)会进一步大幅限制到例如 1000 毫秒。(参考

回答by Adam Bergmark

No, the interval cannot execute until the event loop is cleared, so if you do for instance setInterval(func, 1000); for(;;)then the interval will never run. If other browsers tabs run in the same thread (as they do everywhere(?) except for in chrome, then the same applies if those tabs clog the event loop.)

不,在事件循环被清除之前间隔不能执行,所以如果你这样做,setInterval(func, 1000); for(;;)那么间隔将永远不会运行。如果其他浏览器选项卡在同一线程中运行(就像它们在任何地方(?)除了在 chrome 中所做的那样,那么如果这些选项卡阻塞事件循环,则同样适用。)

But for an interval as large as 60000it is at least very likely that the func will be called in reasonable time. But no guarantees.

但是对于尽可能大的间隔60000,至少很有可能在合理的时间内调用 func。但没有保证。

回答by Richard H

If the tab with the setInterval()function remains open, then yes the function will be executed every 60 seconds, even if you switch to or open other tabs.

如果带有该setInterval()功能的选项卡保持打开状态,则该功能将每 60 秒执行一次,即使您切换到或打开其他选项卡也是如此。

回答by jessegavin

Yeah it works on an example I just created.

是的,它适用于我刚刚创建的示例。

http://jsfiddle.net/5BAkx/

http://jsfiddle.net/5BAkx/

回答by lonesomeday

Yes, the browser's focus is irrelevant.

是的,浏览器的焦点无关紧要。

However, you should not use a string argument to setInterval. Use a reference to the function instead:

但是,您不应将字符串参数用于setInterval. 改用对函数的引用:

setInterval(doSome, 60000);

回答by Emil Vikstr?m

No, you are not guaranteed exact time safety. JS is event based (and single-threeaded) so the event won't fire at the exact right moment, especially not if you have other code running at the same time on your page.

不,您不能保证准确的时间安全。JS 是基于事件的(并且是单三的),因此事件不会在正确的时刻触发,尤其是当您的页面上同时运行其他代码时。

The event will fire in the neighbourhood of the set time value, but not on the exact millisecond. The error may be tens of milliseconds even if no other event is running at the time. This may be an issue if for example you have a long-running process where the timing is important. If you do, you'll need to synchronize with a clock once in a while.

该事件将在设置的时间值附近触发,但不会在精确的毫秒内触发。即使当时没有其他事件正在运行,错误也可能是几十毫秒。例如,如果您有一个时间很重要的长时间运行的流程,这可能是一个问题。如果这样做,您将需要不时与时钟同步。

回答by neebz

Yes it will be called as long as the page is open, regardless the tab is switched or even the browser is minimized.

是的,只要页面打开,它就会被调用,无论选项卡是否切换,甚至浏览器是否最小化。

However make sure you pass the function not a string to setInterval

但是请确保您将函数而不是字符串传递给 setInterval

it should be >

应该是 >

setInterval(doSome, 60000)

setInterval(doSome, 60000)

回答by Ronald Davis

About "exact time safety": The following code starts UpdateAllat intervals of RefreshIntervalmilliseconds, with adjustment each second so that one start occurs at each second at the start of the second. There will be a slight delay for the finite speed of the computer, but errors will not accumulate.

关于“精确时间安全”:以下代码以毫秒UpdateAll为间隔开始,RefreshInterval每秒调整一次,以便在秒开始时每一秒发生一次启动。计算机有限的速度会有轻微的延迟,但错误不会累积。

function StartAtEachSecond ()
{
    var OneSecond  = 1000; // milliseconds
    var MinInteral =   50; // milliseconds, estimated safe interval
    var StartTime = OneSecond - (new Date ()).getMilliseconds (); // Time until next second starts.
    if (StartTime < MinInteral) StartTime += OneSecond
    window.setTimeout (StartAtEachSecond, StartTime + MinInteral); // To set up the second after the next.
    for (var Delay = 0.0; Delay < OneSecond - MinInteral; Delay += RefreshInterval) 
    {
        window.setTimeout (UpdateAll, StartTime + Delay); // Runs during the next second.
    }
}