workaround for FF5 and chrome setInterval(), setTi

2019-03-21 12:34发布

问题:

There is js optimalization causing inactive tabs to slow down setInterval() and setTimeout() in ff5 and chrome. I have it set for 66 miliseconds but it goes up to 1000 whne tab is inactive.

There is way to determine how much time passed when the tab was inactive, but I need js keep running and performing actions, like playing sounds for some events. Is there any way to achive that?

回答1:

The whole point of those clamps is to keep background pages from using too much CPU. What events, exactly do you have going on in 15Hz that need to play sounds even though the user can't see the tab?



回答2:

Mozilla is working on a different way of implementing setInterval and setTimeout, called requestAnimationFrame, perhaps it could help you solve your problem, or atleast hope for future updates that will take care of the problem.

Note that it is an experimental feature, beware of limitations when seeking cross-browser support.

https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame