Why is my setTimeout accelerated when I have several tabs open for my site?

I have a timer that counts down every second. It works fine until the user opens 3 or 4 tabs of my site, after which the newest tab timer will have double or triple speed. Currently, I can only reproduce the error in IE8. I previously used setInterval and could also reproduce the error in Firefox.

I actually use FBJS (Facebook Javascript), so I will just give the pseudo code.

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

However, what I'm really looking for is more theoretical. I know that browsers can try to catch up with setInterval, but how can this affect multiple tabs for setTimeout?

+6
javascript timer settimeout setinterval fbjs
source share
1 answer

This whole situation is very strange. The only scenario that comes to mind when it makes any sense is the one where the browser tries to โ€œsmooth outโ€ the setTimeouts self-healing period, the same as for setInterval, and the code that does this actually confuses the timers in different windows with each other.

I donโ€™t know if this is possible, especially with Facebook, but an interesting test would be to give each instance a randomized name for the countDown function and see if it makes any difference, for example:

 <?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, it speaks of a scenario that I have in mind. (And perhaps provides a way to solve the problem.)

+5
source share

All Articles