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.