Need an inactivity timer

Last post 07-01-2008 7:30 AM by NC01. 3 replies.

Sort Posts:

  • Need an inactivity timer

    06-26-2008, 12:11 PM
    • Member
      452 point Member
    • analogkid
    • Member since 02-13-2006, 8:45 PM
    • Posts 159

    I have a asp.net(2.0) app and I need to come up with a javascript function that will timeout after a certain time period of inactivity.

    To be more specific, if a user has not 1) entered text in a field 2) clicked any links, within the last X minutes, the browser will close.  Could/should this be done like a screensaver where it would be based on keyboard/mouse activity?

    Wondering if someone might point me in the right direction.  I’m guessing I would put this code on the master page, but I’m not sure of the best way to incorporate it on all of my other pages.  My thought is putting a timer on the master page and the code on each page or possibly most controls that would basically “reset the timer”.  Is there a better way of doing this?

  • Re: Need an inactivity timer

    06-26-2008, 2:21 PM
    Answer
    • Participant
      1,465 point Participant
    • aquillin
    • Member since 07-06-2006, 2:06 PM
    • San Antonio, TX
    • Posts 268

    I would add a javascript function that ticks every second, and a global variable that is a count down.  When ever the page is clicked I would reset the count down variable;

    var countDown = 120; //2 minutes 

    function startTimer(){

        window.setTimeout ( "timerTick()", 1000);

    }

    function timerTick(){

         if (countDown == 0){

            window.close();

        return;

        }

        count--;

        window.setTimeout ( "timerTick()", 1000);

    }

  • Re: Need an inactivity timer

    07-01-2008, 1:45 AM
    Answer

    Hi analogkid


    In addition to Aquillin ‘s wonderful code, use 


            window.opener=null
            window.open("","_self")
            window.close();


    Instead of window.close();


    The page can be closed without any alert message.
    Hope this helps!
     
    If I’ve misunderstood your problem, please feel free to let me know.
     
    Thanks.
     

     

    Lance Zhang
  • Re: Need an inactivity timer

    07-01-2008, 7:30 AM
    Answer
    • All-Star
      75,065 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,955
    • TrustedFriends-MVPs

    Why re-invent the wheel? Wouldn't the session timeout variable automatically do that for you? I believe that the default is 20 minutes.
         http://msdn.microsoft.com/en-us/library/ms525473.aspx

    And here is a very good example to use it as you describe:
         http://aspalliance.com/1621_Implementing_a_Session_Timeout_Page_in_ASPNET

    NC...

Page 1 of 1 (4 items)