Chrome:超时/间隔在后台选项卡中暂停? [英] Chrome: timeouts/interval suspended in background tabs?

查看:29
本文介绍了Chrome:超时/间隔在后台选项卡中暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个测试来测试setTimeout的准确性.现在我注意到(正如预期的那样)setTimeout 不是很准确,但对于大多数设备来说并不是非常不准确.现在,如果我在 Chrome 中运行测试并让它在后台选项卡中运行(因此,切换到另一个选项卡并在那里浏览),返回测试并检查结果(如果测试完成),它们会发生巨大变化.看起来超时的运行速度要慢得多.在 FF4 或 IE9 中测试没有发生这种情况.

I was testing the accuracy of setTimeout using this test. Now I noticed that (as expected) setTimeout is not very accurate but for most appliances not dramatically inaccurate. Now if I run the test in Chrome and let it run in a background tab (so, switching to another tab and browse on there), returning to the test and inspecting the results (if the test finished) they are dramatically changed. It looks like the timeouts have been running a lot slower. Tested in FF4 or IE9 this didn't occur.

所以看起来 Chrome 在没有焦点的选项卡中暂停或至少减慢了 javascript 的执行速度.在互联网上找不到关于该主题的太多内容.这意味着我们无法运行后台任务,例如使用 XHR 调用和 setInterval 在服务器上定期检查(我怀疑 setInterval 会看到相同的行为,如果有时间我会写一个测试).

So it looks like Chrome suspends or at least slows down javascript execution in a tab that has no focus. Couldn't find much on the internet on the subject. It would mean that we can't run background tasks, like for example checking periodically on a server using XHR calls and setInterval (I suspect to see the same behavior for setInterval, will write a test if time is with me).

有人遇到过这种情况吗?这种暂停/减速是否有解决方法?你会称它为错误吗,我应该将其归档吗?

Has anyone encountered this? Would there be there a workaround for this suspension/slowing down? Would you call it a bug and should I file it as such?

推荐答案

我最近问过这个问题,这是设计行为.When a tab is inactive, only at a maximum of once per second the function is called.这是代码更改.

I recently asked about this and it is behaviour by design. When a tab is inactive, only at a maximum of once per second the function is called. Here is the code change.

也许这会有所帮助:我该如何制作当 Chrome 中的标签处于非活动状态时,setInterval 是否也有效?

TL;DR:使用Web Workers.

这篇关于Chrome:超时/间隔在后台选项卡中暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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