Chrome: timeouts/interval suspended in background tabs?

KooiInc picture KooiInc · May 17, 2011 · Viewed 95.3k times · Source

I was testing the accuracy of setTimeout using this test. Now I noticed that (as expected) setTimeout is not very accurate but for most appliances not dramatically inaccurate. Now if I run the test in in Chrome and let it run in a background tab (so, switching to another tab and browse on there), returning to the test and inspecting te results (if the test finished) they are dramatically changed. It looks like the timeouts have been running a lot slower. Tested in FF4 or IE9 this didn't occur.

So it looks like Chrome suspends or at least slows down javascript execution in a tab that has no focus. Couldn't find much on the net on the subject. It would mean that we can't run background tasks, like for example checking periodically on a server using XHR calls and setInterval (I suspect to see the same behavior for setInterval, will write a test if time is with me).

Has anyone encountered this? Would there be there a workaround for this suspension/slowing down? Would you call it a bug and should I file it as such?

Answer

pimvdb picture pimvdb · May 17, 2011

I recently asked about this and it is behaviour by design. When a tab is inactive, only at a maximum of once per second the function is called. Here is the code change.

Perhaps this will help: How can I make setInterval also work when a tab is inactive in Chrome?

TL;DR: use Web Workers.