Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 997
Ed Mazur Avatar asked Jan 20 '23 07:01

Ed Mazur


1 Answers

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.

like image 163
tjm Avatar answered Jan 31 '23 08:01

tjm