0

So I have my load script in jQuery:

function load() {
    $.ajax({
        url: '/urltoload.php',
        cache: false,
        success: function(html){
            //do something
        }
    }

Using a setInterval, how can I check if a user is not on the on the page?

Diosney
  • 10,520
  • 15
  • 66
  • 111
user2166538
  • 313
  • 1
  • 3
  • 18
  • I formatted your code, but you're missing `);` and `}` from the end. – Jason P Aug 29 '13 at 17:12
  • 2
    You can't, but you can check if the [window is active](http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active). – adeneo Aug 29 '13 at 17:12

2 Answers2

1

With $(window).focus() you could check if the browser is active.

$(window).focus(function(){
  // interval here
});
S.Visser
  • 4,645
  • 1
  • 22
  • 43
1

If I have correctly understand your question, you can check if the user "is on" the page and when leave it with the following code:

$(document).ready(function(){
   $([window, document]).focusin(function(){
      //Your logic when the page gets active
   }).focusout(function(){
      //Your logic when the page gets inactive
   });
});

Another solution, maybe a little more "intricated", is to check the mouse movements (http://api.jquery.com/mousemove/#fn) each N seconds.

If you have cross-browsing issue, you can check this answer that gives a good workaround for differents browsers: https://stackoverflow.com/a/1060034/1720344

UPDATE
The pseudocode to a timeout function is this:

setTimeout(function() {
      // Do something after 2 seconds
}, 2000);

By your comment, I think is best to "around" the sub-function and not the $(document).ready() (I haven't done before, timeouting the document.ready, but you can try and see what it happens ;) - I believe that this function is simple called after N seconds from the document.ready)

With a timeout of 2 seconds, you can do something like that (but I haven't tested it):

$(document).ready(setTimeout(function(){
       $([window, document]).focusin(function(){
          //Your logic when the page gets active
       }).focusout(function(){
          //Your logic when the page gets inactive
       });
    }, 2000));
Community
  • 1
  • 1
damoiser
  • 6,058
  • 3
  • 40
  • 66
  • @damosier, could this be turned into a function with a setTimeout on the document.ready ? – user2166538 Aug 29 '13 at 17:30
  • @jordanzhninja I have given you a possible solution, but I haven't test it – damoiser Aug 29 '13 at 19:24
  • ok thankan yougen to be honest i have no idea how to do what im trying to do because i need the page title to change when a new comment arrives (found in the mibbit.com ajax irc client) but i dont know how – user2166538 Aug 29 '13 at 19:31
  • @jordanzhninja to change the page title you can call this function __$(document).attr('title', 'new_page_title');__. Checking when a new comment arrives, if the comment is "placed" on another website or server, you can call it with a timeout and check when something change from the previous call (so you know when there is a new comment) – damoiser Aug 30 '13 at 09:16
  • dont worry i have sorted it – user2166538 Aug 30 '13 at 09:56