简体   繁体   English

Web Worker 是否会限制 setTimeout() / setInteval()?

[英]Does Web Worker throttles setTimeout() / setInteval()?

I have a script on foreground tab that starts (dedicated) web worker.我在前台选项卡上有一个脚本,该脚本启动(专用)web 工作人员。 Now I see that setTimeout(xxx, 100) in that web worker is limited to be triggered not more often than once per second instead of 10 times per second as required.现在我看到 web 工作人员中的setTimeout(xxx, 100)被限制为每秒触发一次,而不是根据需要每秒触发 10 次。 I've googled such a limitation for inactive tabs but are there any docs that say the same about Web Workers?我已经用谷歌搜索了非活动标签的这种限制,但是有没有任何文档对 Web Workers 有相同的说法? I've checked this in Chrome and Firefox.我已经在 Chrome 和 Firefox 中检查了这一点。

There is no real specs about that throttling behavior, even though they do allow it :尽管他们确实允许这种限制行为,但没有关于这种限制行为的真正规范:

  1. Optionally, wait a further implementation-defined length of time.可选地,等待进一步的实现定义的时间长度。

This is intended to allow user agents to pad timeouts as needed to optimize the power usage of the device.这旨在允许用户代理根据需要填充超时以优化设备的电源使用。 For example, some processors have a low-power mode where the granularity of timers is reduced;例如,某些处理器具有低功耗模式,其中定时器的粒度会降低; on such platforms, user agents can slow timers down to fit this schedule instead of requiring the processor to use the more accurate mode with its associated higher power usage.在这样的平台上,用户代理可以减慢计时器以适应这个时间表,而不是要求处理器使用更精确的模式以及相关的更高的功耗。

Chrome has this document from 2017 where they expose how they planned to handle background tabs. Chrome 有一份2017 年的文档,他们在其中公开了他们计划如何处理背景标签。

From there we can see a plan section about Web Workers :从那里我们可以看到有关 Web Workers 的计划部分

Throttling web workers (~2018)节流 web 工人(~2018)

We are currently instrumenting web workers to better understand their CPU usage and power impact.我们目前正在检测 web 工作人员,以更好地了解他们的 CPU 使用率和功率影响。 We consider throttling web workers as well, being thoughtful about minimizing broken content.我们也考虑限制 web 工作人员,考虑尽量减少损坏的内容。

So that was scheduled for 2018, and here is the issue tracking it's deployment .所以这是计划在 2018 年, 这里是跟踪它的部署的问题

Should also be noted that there is currently an active discussion going on on the WICG to implement the Page-Lifecycle API .还应该注意的是,目前 WICG 正在进行积极的讨论,以实现页面生命周期 API This API will expose a few events, and a new "frozen" state, which for what we are interested, should also pause Workers' execution .这个 API 将暴露一些事件,以及一个新的“冻结”state,对于我们感兴趣的,它也应该暂停 Workers' execution

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

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