setInterval doesn't slow down on inactive tab

天大地大妈咪最大 提交于 2019-12-10 18:59:15

问题


This is perhaps the opposite of a very common problem: When you use setInterval on one tab and you switch to another, the interval decreases significantly (i.e. slow down). This is very noticeable with audio timing since you can hear it being slowed down.

But on the latest Chrome 56 and Firefox 51, I can not make it happen anymore.

However on Chrome very rarely, and seemingly randomly, switching tabs can have an effect. And it even throttles Web Workers for some reason.

This is causing me headaches because I'm trying to write some Web Audio API stuff that requires very precise timing, and I cannot even see a difference between setInterval and Web Workers, which should be running on its own thread.

Why doesn't setInterval act like it used to and throttle inactive tabs?

Update: Even a setInterval within a Web Worker that has been loaded via a Blob URI, gets throttled in some cases, like switching tabs.


回答1:


Since javascript is a runtime compilation code( Compiled by browser at runtime, browser has to work on it). When you navigate to other tab, the page has it's own scripts and browser has to compile that code as well. So Browser assign a high priority to scripts in active tabs and scripts in remaining tabs are assigned with a lower priority. So it will affect scripts on inactive tabs. However if you have a machine with higher amount of RAM available, this problem will not occur.



来源:https://stackoverflow.com/questions/42151856/setinterval-doesnt-slow-down-on-inactive-tab

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