setInterval不会在非活动选项卡上变慢 [英] setInterval doesn't slow down on inactive tab

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

问题描述

这可能是一个很常见的问题的反面:当你在一个标签上使用 setInterval 并切换到另一个标签时,间隔显着减少(即减慢)。这是非常明显的音频时间,因为你可以听到它减慢。



但在最新的Chrome 56和Firefox 51,我不能再发生了。 / p>

然而,在Chrome浏览器中很少,而且看似随意,切换标签可以起作用。而且它甚至会因为某些原因对Web Worker进行限制。

这让我很头疼,因为我正在编写一些Web Audio API,需要非常精确的时间,甚至无法看到setInterval和Web Workers之间的区别,它们应该在自己的线程上运行。



为什么不是 setInterval setInterval

更新:$ c $>已经通过Blob URI加载,在某些情况下会受到限制,如切换选项卡。

解决方案

由于javascript是一个运行时编译代码(在运行时由浏览器编译,浏览器必须处理它)。当您导航到其他选项卡时,页面具有自己的脚本,浏览器也必须编译该代码。因此,浏览器将高优先级分配给活动选项卡中的脚本,并将其余选项卡中的脚本分配给较低优先级。所以它会影响非活动选项卡上的脚本。但是,如果您的机器具有更高的内存容量,则不会出现此问题。


This is perhaps the opposite of a very common problem: When you use setInterval on one tab and you switch to another, the interval decreases significantly (i.e. slow down). This is very noticeable with audio timing since you can hear it being slowed down.

But on the latest Chrome 56 and Firefox 51, I can not make it happen anymore.

However on Chrome very rarely, and seemingly randomly, switching tabs can have an effect. And it even throttles Web Workers for some reason.

This is causing me headaches because I'm trying to write some Web Audio API stuff that requires very precise timing, and I cannot even see a difference between setInterval and Web Workers, which should be running on its own thread.

Why doesn't setInterval act like it used to and throttle inactive tabs?

Update: Even a setInterval within a Web Worker that has been loaded via a Blob URI, gets throttled in some cases, like switching tabs.

解决方案

Since javascript is a runtime compilation code( Compiled by browser at runtime, browser has to work on it). When you navigate to other tab, the page has it's own scripts and browser has to compile that code as well. So Browser assign a high priority to scripts in active tabs and scripts in remaining tabs are assigned with a lower priority. So it will affect scripts on inactive tabs. However if you have a machine with higher amount of RAM available, this problem will not occur.

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

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