Search code examples
javascripthtmldom-eventsclient-sidesession-timeout

Session timeout in Javascript


I am working on a Javascripyt/html mobile app and I want to implement the session timeout feature. The easiest way is to use the setTimeout function but I want to check if any click, change event was performed before I make the decision. The logic needs to be:

checkForSessionTimeout() {
      if(delta(last logged event time) <  session timeout time) {
              // do not timeout
              // reset the delta to 0
      } else {
              //timeout and take to login page
      }
}

I am able to get the function but not sure how do I log all click and change events on the browser.

Please help!


Solution

  • Are you using jQuery? This example uses jQuery but you should be able to adapt it to whatever you are using: Listen for events on all DOM nodes. With jQuery, $('*').on('click', function() ...). This will let you capture events even when a more close event binding has used stopPropagation.

    It should work the same for change...

    An example (open console): http://jsfiddle.net/u6eDj/2/