How can i stop Javascript from queuing up animations from a setInterval while the page is not the primary tab in view? I've tried clearing the interval on blur of the document but no luck.
Asked
Active
Viewed 188 times
0
-
Take a look at this other SO question: http://stackoverflow.com/questions/1220785/is-it-possible-to-detect-when-a-user-switches-to-a-different-browser-tab that says that `document.onblur()` might be your answer. – jfriend00 Oct 12 '11 at 14:49
3 Answers
0
Have you tried using window.onblur
and window.onfocus
?
They should dot he trick

Martin Jespersen
- 25,743
- 8
- 56
- 68
0
As Martin stated.
Use window.onblur
and window.onfocus
to pause/unpause the setinterval
or atleast the contents inside.
For example.
Use window.onblur
to set var away = true;
Then in the setinterval
you put a if(!away){...your code here...}
this way the animations will not be triggered when the interval passes.
EDIT
Remember to also put a window.onfocus
to set var away = false;

Pangolin
- 7,284
- 8
- 53
- 67
-
I can't get this to work.. i've got window.onblur = clearAnim; function clearAnim() { var away = true; } – Emery King Oct 12 '11 at 15:00
-
Ok, please post your Javascript in the question, especially the part with the setinterval then I will help you from there – Pangolin Oct 12 '11 at 15:07
-
It works good enough with document.onblur... its a little sloppy looking for the first second that you come back to the page but then it stops completely - in chrome – Emery King Oct 12 '11 at 15:07
-
actually no i'm having problems with that now.. It is stopping regardless. apexmarketingconcepts.com - header animation along side of vimeo iframe – Emery King Oct 12 '11 at 15:11
-
Check the EDIT. Remember you must also alter it when the user is back. – Pangolin Oct 12 '11 at 15:35