Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pausing setInterval when page/ browser is out of focus

I have a simple slideshow that I've made on a client's homepage, using setInterval to time the rotations.

To prevent browsers from screwing up setInterval when the page isn't in focus (another tab is being viewed, or another programme), I'm using:

function onBlur() {             clearInterval(play);         };          function onFocus() {              mySlideRotateFunction();          };          if (/*@cc_on!@*/false) {              document.onfocusin = onFocus;             document.onfocusout = onBlur;         } else {             window.onfocus = onFocus;             window.onblur = onBlur;         } 

Where mySlideRotateFunction sets the setInterval and runs some jQuery. While this works the majority of the time, I find that, on occasion, it appears as though onBlur hasn't run, and when I return to the page the timings have 'built up' and the rotations go crazy.

I can't quite determine a cause as to why this happens on occasion, and not on others.

My question- is there a problem with my code, and does anyone have a better suggestion for 'pausing' setInterval when browser window is out of focus?

Thanks

like image 964
Michael Watson Avatar asked Sep 20 '11 10:09

Michael Watson


People also ask

Can you pause a setInterval?

You cannot PAUSE the setInterval function, you can either STOP it (clearInterval), or let it run.

How do I stop browser setInterval?

Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.

Does setInterval slow down browser?

In my experience, running something like this will slow the browser down. You may not notice it at first, but the longer the script is running the worse it will get.


2 Answers

Immediately inside setInterval, place a check to see if the document is focused. The interval will continue firing as usual, but the code inside will only execute if the document is focused. If the window is blurred and later refocused, the interval will have continued keeping time but document.hasFocus() was false during that time, so there's no need for the browser to "catch up" by executing the code block a bunch of times when focus is restored.

var timePerInterval = 7000; $(document).ready(function() {     setInterval(function(){         if ( document.hasFocus() ) {             // code to be run every 7 seconds, but only when tab is focused         }     }, timePerInterval ); }); 
like image 185
Jacob Ford Avatar answered Sep 18 '22 15:09

Jacob Ford


Try something like this:

var myInterval; var interval_delay = 500; var is_interval_running = false; //Optional  $(document).ready(function () {     $(window).focus(function () {         clearInterval(myInterval); // Clearing interval if for some reason it has not been cleared yet         if  (!is_interval_running) //Optional             myInterval = setInterval(interval_function, interval_delay);     }).blur(function () {         clearInterval(myInterval); // Clearing interval on window blur         is_interval_running = false; //Optional     }); });  interval_function = function () {      is_interval_running = true; //Optional      // Code running while window is in focus } 

Some testing done in IE9 and FF6

like image 29
Linus Jäderlund Avatar answered Sep 20 '22 15:09

Linus Jäderlund