Automatic redirect upon session timeout – using Javascript

We had a requirement that the logged in user should be automatically logged out after 5 minutes of inactivity. We tried the session_end event in global.asax but it didnt work as it should have. After thinking a bit me and bryan came up with a simple solution using Javascript which works really nice.


var wintimeout;

function SetWinTimeout() {
 wintimeout = window.setTimeout("window.location.href='../weblogin/default.aspx';",300000); //after 5 mins i.e. 5 * 60 * 1000
 }
 $('body').click(function() {

window.clearTimeout(wintimeout); //when user clicks remove timeout and reset it

SetWinTimeout();

});
 SetWinTimeout();

That is it. Just put the above code in document load ($) and it works like a charm. Just 5 mins away from keyboard and monitor would do a world of good.

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Advertisements

3 Responses to “Automatic redirect upon session timeout – using Javascript”


  1. 1 zahid July 19, 2012 at 1:21 pm

    good job. Thank you.

  2. 2 Corey Mitchell September 7, 2012 at 11:01 am

    Thanks. I wrapped it up into a reusable function:

    WF.Utils.timeout = function () {
    window.global_timeout = window.setTimeout(“document.location.reload();”, 20 * 60 * 1000);
    $(“body”).click(function (e) {
    window.clearTimeout(window.global_timeout);
    WF.Utils.timeout();
    });
    };


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: