Click here to Skip to main content
15,867,453 members
Articles / Web Development / HTML
Article

JavaScript to Show Session Timeout Counter

Rate me:
Please Sign up or sign in to vote.
4.39/5 (37 votes)
25 Mar 2008CPOL1 min read 299.3K   4.9K   58   30
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:

JavaScript
<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:

JavaScript
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

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.

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)


Written By
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

 
Questiondo i need to do this on every page? Pin
varun15018-Jun-15 22:30
varun15018-Jun-15 22:30 
Suggestionsession message is not showing after 5 minute Pin
Member 1018474411-Jun-15 3:28
Member 1018474411-Jun-15 3:28 
QuestionThis is very inaccurate way of determining session timeout Pin
Adam Moszczyński17-Jul-14 0:51
Adam Moszczyński17-Jul-14 0:51 
QuestionProblem when refresh page. Pin
Jayant Patil1-Jul-13 0:38
Jayant Patil1-Jul-13 0:38 
QuestionHelp Please to use in VB.NET Pin
davidand12-Aug-12 15:48
davidand12-Aug-12 15:48 
GeneralThanks Pin
Srinivasa Penta9-Aug-12 22:39
professionalSrinivasa Penta9-Aug-12 22:39 
GeneralMy vote of 5 Pin
hadi55266-Aug-12 20:53
hadi55266-Aug-12 20:53 
QuestionHas nothing to do with session state when page is ajaxified Pin
somealiasname20-Apr-12 6:51
somealiasname20-Apr-12 6:51 
AnswerRe: Has nothing to do with session state when page is ajaxified Pin
THEGAFF26-Apr-12 5:44
THEGAFF26-Apr-12 5:44 
Questionquick question Pin
icelated13-Mar-12 20:04
icelated13-Mar-12 20:04 
AnswerRe: quick question Pin
THEGAFF26-Apr-12 5:40
THEGAFF26-Apr-12 5:40 
QuestionWorking in IE only Pin
demouser74325-Sep-11 22:33
demouser74325-Sep-11 22:33 
AnswerRe: Working in IE only Pin
THEGAFF26-Apr-12 5:36
THEGAFF26-Apr-12 5:36 
GeneralMy vote of 5 Pin
Pankaj Deharia27-Jul-11 23:40
professionalPankaj Deharia27-Jul-11 23:40 
QuestionHow to use this in a user control Pin
Member 8122057-May-10 0:21
Member 8122057-May-10 0:21 
GeneralTime format question Pin
smcirish4-Mar-10 3:33
smcirish4-Mar-10 3:33 
GeneralNince, easy to read Article [modified] Pin
cesar_boucas5-Mar-09 6:53
cesar_boucas5-Mar-09 6:53 
GeneralThank u Pin
jamartinezac5-Jan-09 10:18
jamartinezac5-Jan-09 10:18 
Generaldoubt Pin
jomet5-Jun-08 21:07
jomet5-Jun-08 21:07 
GeneralRe: doubt Pin
farazsk116-Jun-08 18:51
farazsk116-Jun-08 18:51 
Well you can show the alert 2 minutes earlier if you change the if statement like this.

if (sessionTimeout >= 2)

Secondly if your session time out is set to 120 minutes it will automatically start from 120 minutes you don't have to do any thing extra.

If you are talking about showing the count down in an alert or confirm alert, I don't that would be very feasable.

Faraz Shah Khan
MCP, MCAD.Net, MCSD.Net
Blog

GeneralGood Article Pin
Srinath Gopinath14-May-08 19:22
Srinath Gopinath14-May-08 19:22 
Generalnice try, but this can't really work... Pin
henk5360228-Mar-08 23:58
henk5360228-Mar-08 23:58 
GeneralRe: nice try, but this can't really work... Pin
farazsk1129-Mar-08 0:07
farazsk1129-Mar-08 0:07 
GeneralYou got my 5 stars Pin
johnegbert27-Mar-08 8:25
johnegbert27-Mar-08 8:25 
GeneralRe: You got my 5 stars Pin
farazsk1127-Mar-08 15:24
farazsk1127-Mar-08 15:24 

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.