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

为君一笑 提交于 2019-12-01 05:07:12

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6563859/minimum-setinterval-settimeout-delay-on-background-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!