Click here to Skip to main content
14,699,669 members
Articles » Web Development » Client side scripting » General
Posted 26 Mar 2008


57 bookmarked

JavaScript to Show Session Timeout Counter

Rate me:
Please Sign up or sign in to vote.
4.39/5 (37 votes)
26 Mar 2008CPOL
To display the remaining minutes as a counter for a Session to timeout on the Webpage


This article shows how to display the remaining minutes as a counter for a session to timeout on the Webpage that will change after every minute and a message will be displayed when the session timeout will be equal to 0 (zero).

One thing to keep in mind is that the code provided will neither timeout the session or is it responsible for any session related activity. It will just show the timeout counter on the screen depending on the session timeout set within the application. If any additional things are required, additional code has to be written.


It might be possible that in some applications, a developer wants to display the time left for the session to be over or timeout. That will enable the user to save the entered information or it can be used for any other purpose depending on the requirement.

The JavaScript code that will be used to achieve this is as follows:

<script type="text/javascript">
        var sessionTimeout = "<%= Session.Timeout %>";
        function DisplaySessionTimeout()
            //assigning minutes left to session timeout to Label
            document.getElementById("<%= lblSessionTime.ClientID %>").innerText = 
            sessionTimeout = sessionTimeout - 1;
            //if session is not less than 0
            if (sessionTimeout >= 0)
                //call the function again after 1 minute delay
                window.setTimeout("DisplaySessionTimeout()", 60000);
                //show message box
                alert("Your current Session is over.");

The code on page load event to register the JavaScript method on startup is as follows:

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            //set DisplaySessionTimeout() as the startup script of this page
                "onLoad","DisplaySessionTimeout()", true);


Image 1

Figure 1: When the application starts.

Image 2

Figure 2: After 1 minute.

Image 3

Figure 3: When session counter comes to 0, the message box will be displayed.


  • 26th March, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United Arab Emirates United Arab Emirates
Faraz is working as a Senior Software Engineer for a company located in Sharjah, UAE. He likes developing new applications with the latest technologies. Mostly reponsible for web applications using Microsoft.Net. He has done MCPD so far. Other than work play guitars, sing and play PSP.

Comments and Discussions

GeneralSession is blank Pin
obinna_eke27-Mar-08 1:21
Memberobinna_eke27-Mar-08 1:21 
GeneralRe: Session is blank Pin
farazsk1127-Mar-08 16:28
Memberfarazsk1127-Mar-08 16:28 
Generalits useful !! Pin
Niiiissssshhhhhuuuuu26-Mar-08 8:33
MemberNiiiissssshhhhhuuuuu26-Mar-08 8:33 
GeneralHelpful Pin
ryanoc33326-Mar-08 3:35
Memberryanoc33326-Mar-08 3:35 
GeneralRe: Helpful Pin
farazsk1127-Mar-08 16:26
Memberfarazsk1127-Mar-08 16:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.