views:

36

answers:

2

Hi everyone I am working on an Ajax web chat and this application is updated using timed out Ajax requests every few seconds.

Using timed Ajax requests means that the web server has to deal with a very heavy load and i would like to reduce the number of requests as much as possible.

Since the timeout is triggered in the browser i would like to know this:

Is it possible to use JavaScript to detect users that are out browser view so that i can clear the timeout for their chat update?

+2  A: 

Yes, for Gecko and WebKit browsers you can use the onblur event of the global window object:

window.onblur = function(e) {
   //clear timeout here...
};

For IE however, you need to use the onfocusout event.

Jacob Relkin
Thanks for the response, does this event works in all/most browsers?
Q_the_dreadlocked_ninja
+1  A: 

You can check if the "chat box" has focus using

document.activeElement
N 1.1
+1, This can also work but a user can lose focus of the "Chat Box" and still be viewing the window.
Q_the_dreadlocked_ninja