If your code is using setInterval() and you are running in a browser like Firefox or Chrome, then this is because those browsers are slowing down timers in background tabs while they are in the background (and thus not displayed). When you then bring it back to the foreground, it tries to catch them up. There are a number of possible work-arounds. The best idea is to stop your timer when your browser window is no longer displayed and restart it when the window is displayed again.
On the Chromium blog, Google said:
In the forthcoming Chrome 11 release, we plan to reduce CPU
consumption even for pages that are using setTimeout and setInterval.
For background tabs, we intend to run each independent timer no more
than once per second. This change has already been implemented in the
Chrome dev channel and canary builds.
Possible ways to work-around this:
- Stop your timer/animation when the window is not visible. Restart the timerwhen the window becomes visible.
- Instead of setInterval, use setTimeout and then just reset the setTimeout each time it fires to create your own repeating interval.
- Slow your timers down so they don't run afoul of this.
The best option is probably to figure out when to just stop and then restart the timers.
Similar question here: Chrome: timeouts/interval suspended in background tabs?.
FYI, Chrome has new experimental API for detecting page visibility for just this reason. You can read about it here: http://code.google.com/chrome/whitepapers/pagevisibility.html. it helps solve the issue when your page is visible, but doesn't have focus.