By KooiInc

2011-05-17 14:34:00 8 Comments

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?


@Kaan Soral 2018-07-05 12:52:40

Playing an ~empty sound forces the browser to retain the performance - I discovered it after reading this comment: How to make JavaScript run at normal speed in Chrome even when tab is not active?

I need unlimited performance on-demand for a browser game that uses WebSockets, so I know from experience that using WebSockets doesn't ensure unlimited performance, but from tests, playing an audio file seems to ensure it

Here's 2 empty audio loops I created for this purpose, you can use them freely, commercially:

(They include -58db noise, -60db doesn't work)

I play them, on user-demand, with Howler.js:

function performance_trick()
    if(sounds.empty) return;
    sounds.empty = new Howl({
        src: ['/sounds/loops/empty_loop_for_js_performance.ogg','/sounds/loops/empty_loop_for_js_performance.wav'],
        autoplay: true, loop: true,

It's sad that there is no built-in method to turn full javascript performance on/off by default, yet, crypto miners can hijack all your computing threads using Web Workers without any prompt :|

@IVO GELOV 2018-07-10 15:33:52

Excellent idea! Thank you very much!

@Kaan Soral 2018-07-12 06:46:35

Thank you, 58db is very hearable with headphones tho, but muting the site solves that problem

@pimvdb 2011-05-17 14:44:53

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.

@KooiInc 2011-05-17 14:47:36

thanks, I should've looked with 'inactive tab'. Not being a native english speaker is a handicap sometimes.

@pimvdb 2011-05-17 14:48:11

@Kooilnc: No problem :) I'm not a native English speaker either.

@KooiInc 2011-05-17 14:51:35

I suspected that already (dat vermoedde ik al;~)

@gorkemcnr 2017-12-13 15:42:23

I have released worker-interval npm package which setInterval and clearInterval implementation with using Web-Workers to keep up and running on inactive tabs for Chrome, Firefox and IE.

Most of the modern browsers (Chrome, Firefox and IE), intervals (window timers) are clamped to fire no more often than once per second in inactive tabs.

You can find more information on

@Ruslan Tushov 2015-06-28 22:23:07

There is a solution to use Web Workers, because they run in separate process and are not slowed down

I've written a tiny script that can be used without changes to your code - it simply overrides functions setTimeout, clearTimeout, setInterval, clearInterval

Just include it before all your code

@Madara Uchiha 2016-01-04 08:31:23

That's nice and all but be aware that: 1. Workers have no access to the DOM, 2. Workers are only ever executed if they're on a file on their own. It's not a drop-in replacement for setTimeout for a lot of cases.

@Ruslan Tushov 2016-01-22 18:39:55

You are right, but some modern browsers allow to use workers without their own files by using Blobs (‌​rs)

@Madara Uchiha 2016-01-22 19:57:49

Even with that, Web Workers are missing a lot of functionality (namely, DOM) that allows them to be a safe replacement for setTimeout and co.

@Michael 2017-09-01 23:38:11

What about for code that has to run in the front end, for instance, heavy graphics processing tasks that we would like to finish while we do other stuff?

@Carey Estes 2013-07-11 17:29:27

I updated my jQuery core to 1.9.1, and it solved the Interval discrepancy in inactive tabs. I would try that first, then look into other code override options.

@dmi3y 2013-11-08 01:36:30

which version did you upgrade from? I experienced some timeout problems (gallery sliders) with version ~1.6

@Carey Estes 2013-11-08 17:35:28

I was previously on JQ 1.7

@terrorfall 2014-05-29 12:12:57

I'm still experiencing this with 1.10

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] Getting Chrome to accept self-signed localhost certificate

29 Answered Questions

[SOLVED] Disable same origin policy in Chrome

20 Answered Questions

[SOLVED] How do I change Eclipse to use spaces instead of tabs?

26 Answered Questions

[SOLVED] Open a URL in a new tab (and not a new window) using JavaScript

  • 2011-02-05 15:52:13
  • Mark F
  • 2178935 View
  • 1783 Score
  • 26 Answer
  • Tags:   javascript

15 Answered Questions

[SOLVED] "Thinking in AngularJS" if I have a jQuery background?

31 Answered Questions

11 Answered Questions

[SOLVED] How to remove border (outline) around text/input boxes? (Chrome)

4 Answered Questions

[SOLVED] Open link in new tab or window

29 Answered Questions

[SOLVED] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

2 Answered Questions

Sponsored Content