Minimum setInterval()/setTimeout() delay on background tabs [duplicate]
Asked Answered
G

1

5

Possible Duplicate:
Chrome: timeouts/interval suspended in background tabs?

Is there a minimum allowed delay for setInterval() and setTimeout() when being run on a tab you're not currently looking at?

This code runs setInterval() with a specified delay of 100ms and writes out how long the delay actually was. It also reports when you enter/leave the tab.

<html>
<body>
<script type="text/javascript">

window.onfocus = function () { document.body.innerHTML += 'entered tab<br />'; };
window.onblur = function () { document.body.innerHTML += 'left tab<br />'; };
var previous = new Date().getTime();
setInterval(function () {
    var now = new Date().getTime();
    var elapsed = now - previous;
    document.body.innerHTML += elapsed + '<br />';
    previous = now;
}, 100);

</script>
</body>
</html>

Here's an excerpt of the output on Chrome 12.0.742.100 on Ubuntu 10.04.2 LTS:

101
101
101
left tab
1001
1000
1004
1003
1002
1000
entered tab
101
101
101
102
101

I tried different values for the delay too. Any value less than 1000 results in the same behavior of it being raised to 1000 when you're looking at a different tab. Values over 1000 behave correctly. And the same thing happens with the setTimeout() version of this code.

Gannie answered 3/7, 2011 at 15:50 Comment(3)
Interesting. I can confirm the same behaviour in Firefox 5 on Linux.Canopus
Live demo: jsfiddle.net/simevidas/gHZSWNewfeld
This behavior is present in Firefox and Chrome, but not in IE, Safari or Opera...Newfeld
C
10

I know your example was for Chrome, but for Firefox at least, from the Firefox 5 Release Notes (under "What's New in Firefox").

Background tabs have setTimeout and setInterval clamped to 1000ms to improve performance


Oh, and I just realised this has already been asked in regard to chrome, over here: Chrome: timeouts/interval suspended in background tabs?. There's a link there that shows that this is also true in Chrome.

Canopus answered 3/7, 2011 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.