Click here to Skip to main content
6,635,160 members and growing! (14,261 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

JavaScript to Show Session Timeout Counter

By farazsk11

To display the remaining minutes as a counter for a Session to timeout on the Webpage
C# (C# 2.0), Javascript, HTML, .NET (.NET 2.0), ASP.NET
Posted:26 Mar 2008
Views:28,294
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.35 Rating: 3.93 out of 5
2 votes, 8.7%
1
2 votes, 8.7%
2
1 vote, 4.3%
3
5 votes, 21.7%
4
13 votes, 56.5%
5

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)

About the Author

farazsk11


Member
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.
Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralNince, easy to read Article [modified] Pinmembercesar_boucas7:53 5 Mar '09  
GeneralThank u Pinmemberjamartinezac11:18 5 Jan '09  
Generaldoubt Pinmemberjomet22:07 5 Jun '08  
GeneralRe: doubt Pinmemberfarazsk1119:51 6 Jun '08  
GeneralGood Article Pinmembersrinath g nath20:22 14 May '08  
Generalnice try, but this can't really work... Pinmemberhenk536020:58 29 Mar '08  
GeneralRe: nice try, but this can't really work... Pinmemberfarazsk111:07 29 Mar '08  
GeneralYou got my 5 stars Pinmemberjohnegbert9:25 27 Mar '08  
GeneralRe: You got my 5 stars Pinmemberfarazsk1116:24 27 Mar '08  
GeneralSession is blank Pinmemberobinna_eke1:21 27 Mar '08  
GeneralRe: Session is blank Pinmemberfarazsk1116:28 27 Mar '08  
Generalits useful !! PinmemberNiiiissssshhhhhuuuuu8:33 26 Mar '08  
GeneralHelpful Pinmemberinetfly1233:35 26 Mar '08  
GeneralRe: Helpful Pinmemberfarazsk1116:26 27 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project