当我向站点打开多个选项卡时,为什么setTimeout会加快速度? [英] Why does my setTimeout speed up when I have multiple tabs open to my site?

查看:104
本文介绍了当我向站点打开多个选项卡时,为什么setTimeout会加快速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器,它每秒钟倒数一次.在用户打开我的网站的3个或4个标签之前,该标签非常有用,这时最新标签的计时器将变为两倍或三倍速度.我目前只能重现IE8中的错误.我以前使用过setInterval,并且也可以在Firefox中重现该错误.

I have a timer that counts down every second. It works great until the user opens up 3 or 4 tabs of the my site, at which point the newest tab's timer goes double or triple speed. I can currently only reproduce the bug in IE8. I was previously using setInterval, and could reproduce the bug in Firefox as well.

我实际上正在使用FBJS(Facebook的Javascript),所以我只给出一些伪代码.

I'm actually using FBJS (Facebook's Javascript), so I'll just give some pseudocode.

function countDown() {
  ...
  setTimeout(function() { countDown() }, 1000);    
}

countDown();

但是,我真正想要的是理论上的东西.我知道浏览器可以尝试使用setInterval播放追赶"消息,但是多个选项卡如何引起setTimeout的这种行为?

However, what I'm really looking for is more theoretical. I know browsers can try and play "catch up" with setInterval, but how can multiple tabs cause this behaviour for setTimeout?

推荐答案

整个情况很奇怪.想到的唯一有意义的情况是浏览器试图平滑"自恢复setTimeouts的时间段,与setInterval相同,并且这样做的代码实际上使不同窗口中的计时器彼此混淆其他.

That whole situation is very odd. The only scenario that's coming to mind where it makes any sense is one where the browser is trying to "smooth" the period of self-reinstating setTimeouts, same as for setInterval, and the code that's doing that actually confuses timers in different windows with each other.

我不知道这是否可行,尤其是在涉及Facebook的情况下,但是一个有趣的测试是为每个实例赋予countDown函数的随机名称,看看是否有任何区别,例如:

I don't know if it's feasible, especially with Facebook involved, but an interesting test would be to give each instance a randomized name for the countDown function and see if that makes any difference, like:

<?php $timerTag = rand(1, 1000); ?>
function countDown<?php echo $timerTag ?>() {
  ...
  setTimeout(function() { countDown<? php echo $timerTag ?>() }, 1000);    
}

countDown<?php echo $timerTag ?>();

如果这改变了观察到的行为,则表明我想到了这种情况. (并可能提供解决问题的方法.)

If this changes the observed behavior, that argues for the scenario I have in mind. (And possibly provides a way of addressing the problem.)

这篇关于当我向站点打开多个选项卡时,为什么setTimeout会加快速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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