views:

163

answers:

3

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.

+2  A: 

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.

Nate B
I've just tried this and it works (do note that I used jQuery to attach the event handler to onblur).
Jan Hančič
Aside from the fact that `onblur()` should just be `onblur`, I think that could work. However, what I'd really like to do is be able to check whether or not the window currently has focus or not.
Matt Ball
+2  A: 
var 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.

Senthil
why the if(isFocused==true) check?
Jan Hančič
I was using an alert during debugging, when it showed up, it also fired the blur event and it was a total mess :D. Also, Firefox doesn't recognize and notify javascript about tab switching in real-time very accurately. (I know from experience developing addons) Sometimes, it is possible for these events to fire twice etc... Depending on your code, this may be harmless or completely screw you up. So, I am just making sure that you set the variable to 'unfocussed' only if it is focussed now. Why change it to 'unfocussed' when it is already 'unfocussed'? Get what Im saying?
Senthil
I see. But the if statement only makes sense if you are actually doing something inside of it (something that would screw your page if you'd execute it twice in a row). In this concrete case it's totally redundant, as it won't matter if the variable is set twice to the same value ...
Jan Hančič
You're right. A little over-kill for the original poster's question. Maybe I had a very bad experience with this, that I am doing these checks subconsciously :D
Senthil
More to the point, the check *would* just be `if(isFocused)`.
Matt Ball
@Bears: no, if you'd want to make that 100% correct, you would use isFocused === true :)
Jan Hančič
+1  A: 

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.

Fred