Click here to Skip to main content
Click here to Skip to main content

JavaScript to Show Session Timeout Counter

, 26 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
To display the remaining minutes as a counter for a Session to timeout on the Webpage

Introduction

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.

Background

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 = sessionTimeout - 1;
            
            //if session is not less than 0
            if (sessionTimeout >= 0)
                //call the function again after 1 minute delay
                window.setTimeout("DisplaySessionTimeout()", 60000);
            else
            {
                //show message box
                alert("Your current Session is over.");
            }
        }
</script>

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
            Page.ClientScript.RegisterStartupScript(this.GetType(),
                "onLoad","DisplaySessionTimeout()", true);
        }
    }

Screenshots

Figure 1: When the application starts.

Figure 2: After 1 minute.

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

History

  • 26th March, 2008: Initial post

License

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

Share

About the Author

farazsk11
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

 
QuestionThis is very inaccurate way of determining session timeout PinmemberAdam Moszczyński17-Jul-14 1:51 
QuestionProblem when refresh page. PinmemberJayant Patil1-Jul-13 1:38 
QuestionHelp Please to use in VB.NET Pinmemberdavidand12-Aug-12 16:48 
GeneralThanks Pinmemberpsnivas9-Aug-12 23:39 
GeneralMy vote of 5 Pinmemberhadi55266-Aug-12 21:53 
QuestionHas nothing to do with session state when page is ajaxified Pinmembersomealiasname20-Apr-12 7:51 
AnswerRe: Has nothing to do with session state when page is ajaxified PinmemberTHEGAFF26-Apr-12 6:44 
Questionquick question Pinmembericelated13-Mar-12 21:04 
AnswerRe: quick question PinmemberTHEGAFF26-Apr-12 6:40 
QuestionWorking in IE only PinmemberDorababu74325-Sep-11 23:33 
AnswerRe: Working in IE only PinmemberTHEGAFF26-Apr-12 6:36 
GeneralMy vote of 5 PinmemberPankaj Deharia28-Jul-11 0:40 
QuestionHow to use this in a user control PinmemberMember 8122057-May-10 1:21 
GeneralTime format question Pinmembersmcirish4-Mar-10 4:33 
GeneralNince, easy to read Article [modified] Pinmembercesar_boucas5-Mar-09 7:53 
GeneralThank u Pinmemberjamartinezac5-Jan-09 11:18 
Generaldoubt Pinmemberjomet5-Jun-08 22:07 
GeneralRe: doubt Pinmemberfarazsk116-Jun-08 19:51 
GeneralGood Article Pinmembersrinath g nath14-May-08 20:22 
Generalnice try, but this can't really work... Pinmemberhenk5360229-Mar-08 0:58 
GeneralRe: nice try, but this can't really work... Pinmemberfarazsk1129-Mar-08 1:07 
GeneralYou got my 5 stars Pinmemberjohnegbert27-Mar-08 9:25 
GeneralRe: You got my 5 stars Pinmemberfarazsk1127-Mar-08 16:24 
GeneralSession is blank Pinmemberobinna_eke27-Mar-08 1:21 
GeneralRe: Session is blank Pinmemberfarazsk1127-Mar-08 16:28 
Generalits useful !! PinmemberNiiiissssshhhhhuuuuu26-Mar-08 8:33 
GeneralHelpful Pinmemberinetfly12326-Mar-08 3:35 
GeneralRe: Helpful Pinmemberfarazsk1127-Mar-08 16:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 26 Mar 2008
Article Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid