I'm writing a webapp that performs an action every minute or so which (very briefly) hangs the browser. I'd like to pause this action when the tab displaying the webapp is not shown, to minimize the annoyance. Is there any way to do this using Javascript, under the latest version of Firefox?
Edit: to clarify, I'm asking about how to determine the visibility of the tab that some JS code is running in - not how to pause/resume the action which hangs the browser.
I'm writing a webapp that performs an action every minute or so which (very briefly) hangs the browser. I'd like to pause this action when the tab displaying the webapp is not shown, to minimize the annoyance. Is there any way to do this using Javascript, under the latest version of Firefox?
Edit: to clarify, I'm asking about how to determine the visibility of the tab that some JS code is running in - not how to pause/resume the action which hangs the browser.
Share Improve this question edited Dec 3, 2009 at 16:51 Matt Ball asked Dec 3, 2009 at 16:39 Matt BallMatt Ball 360k102 gold badges653 silver badges720 bronze badges3 Answers
Reset to default 7var isFocused = true;
window.onblur=function(){
if(isFocused==true){
isFocused=false;
}
}
window.onfocus = function(){
isFocused = true;
}
Now, that action you perform, every minute or two, do it only when isFocused is true. That is when the tab of your page/webapp is focused.
Not tested, but how about this:
window.onblur() = function () {
... pause your script here
}
I don't know if Firefox handles the window blur in tabs as separate windows.
I believe you will need to make do with the window.onblur event, because accessing the browser window from a script in a webpage is forbidden for security reasons (only allowed for privileged scripts).
What you want to do (access the browser window from within a child window or a webpage) is described in Mozilla Developer Center, but it does mention there that only a privileged script can do it, and you'll probably get a "Premission denied" error when you try.