Created
August 23, 2013 17:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************** | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
am using this. brilliant :)