后台选项卡上的最小setInterval()/setTimeout()延迟 [英] Minimum setInterval()/setTimeout() delay on background tabs
问题描述
在当前未查看的选项卡上运行时, setInterval()
和 setTimeout()
是否有最小允许的延迟?
Is there a minimum allowed delay for setInterval()
and setTimeout()
when being run on a tab you're not currently looking at?
此代码以指定的100ms延迟运行 setInterval()
,并写出延迟实际上有多长时间.当您输入/离开标签时,它也会报告.
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>
以下是Ubuntu 10.04.2 LTS上Chrome 12.0.742.100的输出摘录:
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
我也尝试了不同的延迟值.小于1000的任何值都将导致相同的行为,即当您查看其他选项卡时,该值会升至1000.值超过1000的行为正确.此代码的 setTimeout()
版本也会发生同样的事情.
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.
推荐答案
我知道您的示例适用于Chrome,但至少适用于Firefox,请访问
I know your example was for Chrome, but for Firefox at least, from the Firefox 5 Release Notes (under "What's New in Firefox").
背景"标签具有setTimeout和setInterval钳位到1000ms提高性能
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.
这篇关于后台选项卡上的最小setInterval()/setTimeout()延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!