简体   繁体   English

javascript setInterval

[英]javascript setInterval

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? 我是否安全,即使我在浏览器中更改选项卡,也会每60秒触发一次doSome()函数?

Passing a string to setInterval is fine , and is one of two ways to use setInterval , the other is passing a function pointer. 将字符串传递给setInterval很好 ,并且是使用setInterval的两种方法之一,另一种是传递函数指针。 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('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 doSome is local to some non-global scope, calling the latter from within the same scope will run the local doSome at 60000ms intervals. 如果doSome是某些非全局范围的本地,则在同一范围内调用后者将以60000ms的间隔运行本地doSome Calling the former code will always look for doSome in the global scope, and will fail if there is no doSome function in the global scope. 调用前一个代码将始终在全局范围内查找doSome ,如果全局范围内没有doSome函数,则会失败。

The function will reliably be triggered, regardless of tab focus, at intervals of at least 60000ms , but usually slightly more due to overheads and delays. 无论标签焦点如何,都会以至少 60000ms的间隔可靠地触发该功能,但由于开销和延迟,通常会略微更多。

All browsers clamp the interval value to 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 setInterval drastically to eg 1000ms if the tab is not focused. 请注意,某些浏览器(即将推出的Firefox 5是一个,但可能还有其他我不知道的)如果选项卡没有聚焦,则会进一步将setInterval钳制到例如1000ms。 ( Reference ) 参考

No, the interval cannot execute until the event loop is cleared, so if you do for instance setInterval(func, 1000); for(;;) 不,在清除事件循环之前,间隔不能执行,因此如果您执行setInterval(func, 1000); for(;;) setInterval(func, 1000); for(;;) then the interval will never run. setInterval(func, 1000); for(;;)那么间隔永远不会运行。 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.) 如果其他浏览器选项卡在同一个线程中运行(因为它们在任何地方(?)除了在chrome中,那么如果这些选项卡阻塞了事件循环,则同样适用。)

But for an interval as large as 60000 it is at least very likely that the func will be called in reasonable time. 但是对于大到60000的间隔,至少很可能在合理的时间内调用func。 But no guarantees. 但没有保证。

如果具有setInterval()函数的选项卡保持打开状态,那么即使您切换到或打开其他选项卡,该函数也会每60秒执行一次。

About "exact time safety": The following code starts UpdateAll at intervals of RefreshInterval milliseconds, with adjustment each second so that one start occurs at each second at the start of the second. 关于“确切时间安全”:以下代码以RefreshInterval毫秒为间隔启动UpdateAll ,每秒调整一次,以便在第二秒开始时每秒启动一次。 There will be a slight delay for the finite speed of the computer, but errors will not accumulate. 计算机的有限速度会略有延迟,但错误不会累积。

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.
    }
}

Yeah it works on an example I just created. 是的,它适用于我刚创建的一个例子。

http://jsfiddle.net/5BAkx/ http://jsfiddle.net/5BAkx/

Yes, the browser's focus is irrelevant. 是的,浏览器的重点是无关紧要的。

However, you should not use a string argument to setInterval . 但是,您不应该对setInterval使用字符串参数。 Use a reference to the function instead: 改为使用对函数的引用:

setInterval(doSome, 60000);

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. 如果这样做,您需要偶尔与时钟同步。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM