Skip to content

Instantly share code, notes, and snippets.

@ankurshukla
Created August 23, 2013 17:19
Show Gist options
  • Save ankurshukla/6321797 to your computer and use it in GitHub Desktop.
Save ankurshukla/6321797 to your computer and use it in GitHub Desktop.
Session Timeout Implementation in JavaScript - particularly useful for mobile and smart (hybrid) clients that have a session management implementation at the server side
/**********************************************************
* SESSION TIMEOUT IMPLEMENTATION FROM JAVASCRIPT *
* ********************************************************/
function Session() {}
Session.prototype.init = function() {
console.log('inside Session.init()');
//capturing all click, touch and keypress events
window.addEventListener('touchstart',Timeout,false);
window.addEventListener('click',Timeout,false);
window.addEventListener('keypress', Timeout, false);
function _timeout(){
return function() {
//implement your logic here to make
//a server side call (REST maybe?)
//that kills the server side sessiom
}
}
function Timeout() {
console.log('inside goTimeout()');
if(typeof(timer) != 'undefined'){
console.log("clearing timer");
timer = clearTimeout(timer); //reset as soon as something is clicked
}
timer = setTimeout(_timeout(), 5000 /*test tiemout period in millisec*/);
}
}
var sessionTimeout = new Session();
sessionTimeout.init();
@atorscho
Copy link

timer is undefined.

@marsojane
Copy link

am using this. brilliant :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment