开发者

How to watch time without activity and interact with asp.net page

开发者 https://www.devze.com 2023-02-08 12:58 出处:网络
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.

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开发者_StackOverflow中文版 you are inactive for longer periods, if you dont react they will automatically logout you.


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.


Try Session Time Out Tricks by Joe Stagner

0

精彩评论

暂无评论...
验证码 换一张
取 消