Reputation: 2697
Let's say we have 30 mins session timeout.
What is the best way to track user and interact with the page when there is 1 minute till sessions timeouts.
I have found this JQuery solution: link
But I am not sure it is exactly what I need. This will reset if user moves with mouse. I need to track activity such as requesting another page or refreshing page.
Something like godaddy.com has if you are inactive for longer periods, if you dont react they will automatically logout you.
Upvotes: 0
Views: 361
Reputation: 65166
All you need to keep the session alive is to have a really simple timer on the page that's started on page load. You don't need jQuery for the timer, but it's handy for refreshing the session:
setInterval(function() {
$.get("/keepalive.ashx");
}, 29 * 60 * 1000); // time in milliseconds
If you'd rather end the session, just change it into a redirect:
setTimeout(function() {
location.href = "/logout.aspx";
}, 29 * 60 * 1000);
As timers don't survive over page loads, it won't log the user out if they're still active. If you have some other things on the page that you would like to reset the timeout, it's probably the best to make a function out of it:
var sessionTimer;
function resetSessionTimeout() {
if (sessionTimer)
clearTimeout(sessionTimer);
var sessionTimer = setTimeout(...);
}
And call that whenever you want the session timeout to be reset.
Upvotes: 1