tags:

views:

56

answers:

3

Hi everyone,

I want to write javascript that will kill sestion on the web page if user did not make any action for some time taken from configuration. How can I know that user did not make any actions by using jQuery.

Thanks a lot.

+1  A: 

To check, that a user didn't do anything, you could watch for events that signify user interaction:

var last_seen = 0;
var timeout = null;
$('body').mousemove(function () {
  last_seen = (new Date()).getTime();
  window.clearTimeout(timeout);
  timeout = window.setTimeout(clear_da_session, 10000);
});
/* ...  and likewise for things like
   $('input').focus();
   $('a').click();
   and 'keypress' events
*/

The clearTimeout and setTimeout stuff takes care of something (i.e. the clear_da_session function) happening after some time of not firing any of the listed events.

However, I want to re-emphasize my comment from above: Don't do this at home, kids! Use whatever your serverside language has on board. This is by far more reliable than trying to track something, that might be untrackable.

Boldewyn
ahh, you beat me to it :)
Jeriko
i think is better to check for body mousemove just at some interval of time not everytime! ;-)
aSeptik
@aSeptik: How would you do this? If you check, e.g., at second 5 to 6 for mouse moves, but the user moves it in seconds 6 to 10, then you fire the wrong behaviour at second 10. And, yes, it *is* a performance nightmare, but hey, who am I to question the OP's reasons.
Boldewyn
hey bro i'm with you at 100%, i have only saied to try to reduce the nightmare for fun! ;-)
aSeptik
+3  A: 

You could trap the mousedown and keydown events for the entire document, and then setup a timeout to run if the events are not raised within a certain timeframe:

<html>
  <head>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>

    <script type="text/javascript">
        var _idleEventId = null;
        var _idleMaxMilliSeconds = 10000;

        function OnIdle() {
            alert('You\'re idle!');
        }

        $(document).bind("mousedown keydown", function() {
            clearTimeout(_idleEventId);
            _idleEventId = setTimeout(OnIdle, _idleMaxMilliSeconds);
        });

        $(document).ready(function() {
            _idleEventId = setTimeout(OnIdle, _idleMaxMilliSeconds);
        });
    </script>
  </head>
  <body>
    Hello World
  </body>
</html>
Fredrik Johansson
Thank you, but what with all other events like mouse move, mouse click or any other user activity?
SonOfOmer
just add them as a bind parameter: bind("mousedown keydown mousemove etc"), find the entire list at http://api.jquery.com/category/events/
Fredrik Johansson
+2  A: 

how to use cookie just in case: http://www.w3schools.com/JS/js_cookies.asp

then i would have like this

NOTE: just a proof of concept not tested!

setInterval("checkForActivity()", 5000); //set a reasonable time..

function checkForActivity() {
    var user_has_moved_at = (new Date()).getTime(); //generate a time
    var time_elapsed = getCookie( COOKIE_NAME ); //get a time from previous stored
    //check how many time is passed from last move
    if ( ( user_has_moved_at - time_elapsed ) < 3600 ) { 
     //less then 1 hour.. user is still here..
        $(document.body).bind('mousemove',
        function() {
         // so update the fresh air...
            setCookie( COOKIE_NAME , user_has_moved_at);
         // unbind event
            $(document.body).unbind('mousemove');
        });

    } else {
     // more then 1 hour... destroy cookie... user is out
        setCookie(COOKIE_NAME, null); //destroy cookie
    }

};
aSeptik
I must admit, it's a nice trick. +1
Boldewyn

related questions