Does Web Worker throttles setTimeout() / setInteval()?

孤街醉人 提交于 2020-06-29 04:28:07

问题


I have a script on foreground tab that starts (dedicated) web worker. 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. I've googled such a limitation for inactive tabs but are there any docs that say the same about Web Workers? I've checked this in Chrome and Firefox.


回答1:


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.

From there we can see a plan section about Web Workers:

Throttling web workers (~2018)

We are currently instrumenting web workers to better understand their CPU usage and power impact. We consider throttling web workers as well, being thoughtful about minimizing broken content.

So that was scheduled for 2018, and here is the issue tracking it's deployment.

Should also be noted that there is currently an active discussion going on on the WICG to implement the Page-Lifecycle API. This API will expose a few events, and a new "frozen" state, which for what we are interested, should also pause Workers' execution.



来源:https://stackoverflow.com/questions/61364663/does-web-worker-throttles-settimeout-setinteval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!