选项卡/窗口处于非活动状态时,setInterval变慢 [英] setInterval slows down with tab/window inactive

查看:188
本文介绍了选项卡/窗口处于非活动状态时,setInterval变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个Web应用程序,我使用setInterval和500ms计时器作为一些时钟。

I build a web app and I use setInterval with 500ms timer for some clock.

当窗口处于活动状态时,时钟运行完美,我用它:

When the window is active the clock runs perfect, I use that:

var tempTimer = 0;
setInterval(function () {
    goTimer()
}, 500);

function goTimer() {
    tempTimer++;
    $("#timer").val(tempTimer);
}

但问题是窗口/标签变为非活动状态 - 间隔时间改变到1000毫秒!

But the problem is when the window/tab becomes inactive - the interval is changed to 1000ms!

当我再次聚焦窗口/标签时,它会变回500毫秒。

When i focus the window/tab again, it changes back to 500ms.

检查这个out: http://jsfiddle.net/4Jw37/

非常感谢。

推荐答案

是的,这种行为是故意的。

Yes, this behavior is intentional.

请参阅 MDN文章


在(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2)和Chrome 11中,
超时被限制为不会超过一次非活动标签中每秒
(1000毫秒);有关
的详细信息,请参阅错误633421 或在 crbug.com/66078 有关Chrome中的详细信息。

In (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) and Chrome 11, timeouts are clamped to firing no more often than once per second (1000ms) in inactive tabs; see bug 633421 for more information about this in Mozilla or crbug.com/66078 for details about this in Chrome.

spec 说:


注意:此API不保证计时器将完全按
计划运行。由CPU负载,其他任务等引起的延迟预计为

Note: This API does not guarantee that timers will run exactly on schedule. Delays due to CPU load, other tasks, etc, are to be expected.

这篇关于选项卡/窗口处于非活动状态时,setInterval变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆