1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Detecting if user is AFK

Discussion in 'JavaScript' started by drewbe121212, Feb 7, 2008.

  1. #1
    With the mass use of AJAX lately in web applications, I have come across an interesting thing. I am going to be designing an AJAX based script that will need almost every user on the site to be continually polling at approximately every second to three seconds. However, if this user is not currently active with the page (left there computer for whatever reason, but left the webpage up), I want to keep the AJAX from continually spinning (polling) until they return. When they return the new request and obtain all the information they missed in there absense.

    Does anyone know anything good or an article related close to this? I have google'd and found nothing.

    What I am thinking is that to be AFK it would mean that there mouse nor keys have been pressed/moved.

    I guess a very crude way to do this would be to reset a countdown timer every single time a key is pressed or a mouse is pressed, and if that countdown timer should reach 0 set the user AFK and stop the polling until they return.

    Please let me know your thoughts on this, and some ideas you may have for this. The user will be able to type very agressively if needed on this page so I wonder how the constant reseting of a timer would be with constant key typing and mouse pressing, and if it could cause some issues.

    A Listener could be another alternative as well...

    Please let me know your thoughts / ideas! Because this will be a very interesting beast for me.
     
    drewbe121212, Feb 7, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    That's an interesting idea, one idea is to watch the window.onfocus() and window.onblur() event's- if someone were to leave their computer while your ajax is still running, then (in most cases) a screen saver would popup, taking focus away from the browser (?), then you pause your ajax script. Otherwise I think that a timer might be your only other way, if the user is sending input then you can reset the timer on that. . .
     
    ToddMicheau, Feb 7, 2008 IP
  3. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    more or less this has been mentioned.
    but..

    onload: set timeout = 0

    timer: timeout += seconds

    if timeout = 30 seconds (or whatever you think is enough time for someone to be AFK)
    user is AFK
    end

    onmousemove or onkeydown set timeout = 0


    (sorry for the pseudo code, this is how my mind works)
     
    imvain2, Feb 7, 2008 IP
  4. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #4
    ~Todd - that window.focus is another thing I may use as a seperate qualifier for this. If the AJAX is spinning, but all of a sudden window looses focus, I could start another seperate timer (say 1 minute instead of 5 minutes) that when it reaches 0 sets the user to afk. This way incase the user is browsing around on another window/tab it isn't continually polling me. However this raises the issue of needing to continually check if the browser window is focused or not.

    ~imvain2 - Yup; that was the general idea I had.

    Although once the user is AFK as soon as there is a mouse move / key press I would return the user back.



    Here is what I am thinking with the window focus and timer psudo code:

    expireTime = 60;
    expireTimeFocus = 300;

    time = 0;
    function timer()
    {
    time = time + 5;
    if (window.focused)
    {
    if (time >= expireTimeFocus) { setAFK(); }
    }
    else
    {
    if (time => expireTime { setAFK(); }
    }
    }
    // Only spin it every 5 seconds, to keep from overloading the browsers memory
    intTimer = setInterval('timer()', 5000);

    function setAFK()
    {
    stopInterval(intTimer);
    AJAX.request> Update AFK status for other users to download it on there next request
    }



    One of the things I may do though is while not completely stop the polling during 'afk', but slow it down to say every minute or so, since my server keeps track of when there last poll was all the information that came in there non-polling will be prevailant.
     
    drewbe121212, Feb 8, 2008 IP