Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using setInterval, if I switch tabs in Chrome and go back, the slider goes crazy catching up

I have a jQuery slider on my site and the code going to the next slide is in a function called nextImage. I used setInterval to run my function on a timer, and it does exactly what I want: it runs my slides on a timer. BUT, if I go to the site in Chrome, switch to another tab and return, the slider runs through the slides continuously until it 'catches up'. Does anyone know of a way to fix this. The following is my code.

setInterval(function() { nextImage(); }, 8000); 
like image 940
Nathan Shubert Avatar asked May 31 '11 05:05

Nathan Shubert


People also ask

Why you should not use setInterval?

In case of time intensive synchronous operations, setTimeInterval may break the rhythm. Also, if any error occurs in setInterval code block, it will not stop execution but keeps on running faulty code. Not to mention they need a clearInterval function to stop it.

Does setInterval run in background tab?

Timers methods setTimeout() / setInterval() running on background tabs can be resource exhausting. An application running callbacks at very short intervals in a background tab may drain a lot of memory to the point that the working of the currently active tab may be impacted.

Does setInterval run when tab is not active?

Both setInterval and requestAnimationFrame don't work when tab is inactive or work but not at the right periods.

Which is better setTimeout or setInterval?

Nested setTimeout calls are a more flexible alternative to setInterval , allowing us to set the time between executions more precisely. Zero delay scheduling with setTimeout(func, 0) (the same as setTimeout(func) ) is used to schedule the call “as soon as possible, but after the current script is complete”.


2 Answers

How to detect when a tab is focused or not in Chrome with Javascript?

window.addEventListener('focus', function() {     document.title = 'focused'; },false);  window.addEventListener('blur', function() {     document.title = 'not focused'; },false); 

To apply to your situation:

var autopager; function startAutopager() {     autopager = window.setInterval(nextImage, 8000); } function stopAutopager() {     window.clearInterval(autopager); }  window.addEventListener('focus', startAutopager);     window.addEventListener('blur', stopAutopager); 

Note that in the latest version of Chromium, there is either a bug or a 'feature' which is making this less reliable, requiring that the user has clicked at least once anywhere in the window. See linked question above for details.

like image 185
ninjagecko Avatar answered Oct 06 '22 00:10

ninjagecko


I post an answer here: How can I make setInterval also work when a tab is inactive in Chrome?

Just do this:

setInterval(function() {     $("#your-image-container").stop(true,true);     nextImage();  }, 1000); 

inactive browser tabs buffer some of the setInterval or setTimeout functions. stop(true,true) - will stop all buffered events and execute immadietly only last animation.

The window.setTimeout() method now clamps to send no more than one timeout per second in inactive tabs. In addition, it now clamps nested timeouts to the smallest value allowed by the HTML5 specification: 4 ms (instead of the 10 ms it used to clamp to).

like image 22
www Avatar answered Oct 06 '22 00:10

www