1

I'm currently developing a small conversation system.

When a user writes a new message, the title of the page changes to New Message, which is intended.

However, if the window is active when the message is received, the title should not be updated.

Currently, it will display the New Message regardless of the window is active or not, and only removes it again when a user changes tab and back.

How do I make it only run the if (newCount !== messagesCount) function if the window is not active?

Here's the current code:

if (newCount !== messagesCount) {
    document.title = 'New Message - ' + title;
}

$(window).focus(function() {
    document.title = title;
});
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
  • 1
    Try looking here - http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active and here - http://stackoverflow.com/questions/5766263/run-settimeout-only-when-tab-is-active – Mark Walters Jan 25 '13 at 16:25

1 Answers1

1

You can try to track the window status listening to onfocus and onblur windows event, something like:

var hasFocus = true;
window.onfocus = function(){ hasFocus = true;}
window.onblur = function(){ hasFocus = false;}

and check in your method the value of hasFocus

mdn
  • 252
  • 1
  • 6