简体   繁体   English

setTimeout函数的顺序是0、0、0,但也有0,1,0,怎么办?

[英]setTimeout functions with 0, 0, 0 in sequence but also 0,1,0 how?

I just tried executing below code 我只是尝试执行以下代码

 setTimeout(function(){ console.log(1); }, 0);
 setTimeout(function(){ console.log(2); }, 0);
 setTimeout(function(){ console.log(3); }, 0);

Above code has logged 1, 2, 3 in sequence. 上面的代码按顺序记录了1、2、3。 But if I do the same thing with seconds values 0, 1, 1 但是如果我用秒值0、1、1做同样的事情

setTimeout(function(){ console.log(1); }, 0);
setTimeout(function(){ console.log(2); }, 1);
setTimeout(function(){ console.log(3); }, 0);

Still the above code logs same 1, 2, 3 in sequence. 上面的代码仍然按顺序记录相同的1、2、3。 Tested this in Chrome! 在Chrome中对此进行了测试!

Would like to know whether this is happening due to console.log() . 想知道是否由于console.log()而发生。 Any inputs on this? 有什么意见吗?

The HTML standard does not require user agents to strictly order queued timer events by their intended wait time when said wait time has already elapsed. HTML标准不要求用户代理在所述等待时间已经过去时,按其预期的等待时间严格排序排队的计时器事件。 It is very likely that the third call to setTimeout , returning to native code, and running the first JS callback from the event loop takes more than 1 ms, so running the second callback immediately upon re-entering the event loop next is just as logical as running the third. 很有可能第三次调用setTimeout ,返回本机代码并从事件循环运行第一个JS回调要花费1毫秒以上的时间,因此在再次进入事件循环后立即运行第二个回调与逻辑相同作为第三。

The resolution of context-switching in an operating system is typically greater than 1ms (expect 5-10ms or so); 操作系统中上下文切换的分辨率通常大于1毫秒(预计5到10毫秒左右); therefore, you cannot expect a difference of 1ms to be noticeable. 因此,您不能期望相差1ms会引起注意。

Notwithstanding the above, in terms of your browser's JavaScript engine, historically speaking and now officially as of HTML5, any difference below 4ms is likely to be ignored anyway: 尽管如此,就您的浏览器的JavaScript引擎而言,从历史上讲,从HTML5开始正式使用,现在4ms以下的任何差异都可能会被忽略

Historically browsers implement setTimeout() "clamping": successive setTimeout() calls with delay smaller than the "minimum delay" limit are forced to use at least the minimum delay. 历史上,浏览器实现setTimeout()的“钳位”:延迟小于“最小延迟”限制的连续setTimeout()调用至少被迫使用最小延迟。 The minimum delay, DOM_MIN_TIMEOUT_VALUE , is 4 ms (stored in a preference in Firefox: dom.min_timeout_value ), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL of 5ms. 最小延迟DOM_MIN_TIMEOUT_VALUE为4毫秒(存储在Firefox中的首选项中: dom.min_timeout_value ), DOM_CLAMP_TIMEOUT_NESTING_LEVEL为5毫秒。

In fact, 4ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward. 实际上, HTML5规范指定了 4毫秒并且在2010年及以后发布的浏览器中保持一致。 Prior to (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested timeouts was 10 ms. 在(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2)之前,嵌套超时的最小超时值为10 ms。

In addition to "clamping", the timeout can also fire later when the page (or the OS/browser itself) is busy with other tasks. 除了“夹紧”之外,当页面(或OS /浏览器本身)忙于其他任务时,超时还会在以后触发。

With all the timers firing during the same event tick, then, the only remaining ordering to go on is the order in which the timers were set, which results in 1, 2, 3 . 在所有计时器在同一事件滴答期间触发的情况下,剩下的唯一排序顺序就是计时器的设置顺序,结果是1, 2, 3 Some browsers though, like Firefox, do honour the implicit ordering implied by the requested delays (even when those delays are rounded up to the same value) and in those browsers you will see 1, 3, 2 . 但是,某些浏览器(如Firefox)确实遵守请求的延迟所隐含的隐式顺序(即使将这些延迟四舍五入为相同的值),在这些浏览器中,您会看到1, 3, 2

某些浏览器(例如,较旧版本的Internet Explorer)不支持10ms以下的计时器分辨率,因此它们会将1ms超时视为0ms。

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

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