3

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.

2
  • 3
    Why do you want to do this on client side? Your server-side language should already be well equipped with a mechanism to invalidate sessions. Commented May 25, 2010 at 12:18
  • 1
    Set the cookie timeout when the session is created? Commented May 25, 2010 at 12:22

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but what with all other events like mouse move, mouse click or any other user activity?
just add them as a bind parameter: bind("mousedown keydown mousemove etc"), find the entire list at api.jquery.com/category/events
2

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
    }

};

Comments

1

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.

3 Comments

i think is better to check for body mousemove just at some interval of time not everytime! ;-)
@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.
hey bro i'm with you at 100%, i have only saied to try to reduce the nightmare for fun! ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.