0

I had inserted a countdown timer in my project. When I run it , the timer start but when I stop debugging it and after some time when I again run it then what I notice is, the timer is still running. For example, I have given time of 3 minutes to the timer i.e. after 3 minutes it should redirect to the result page...now I run the page and the count down timer is started...when I stop debugging it suppose at 00:02:45(time left) and after 5 seconds when I again run it then the timer continues with 00:02:40(time left) . I don't want like this to be happen . How do I stop the continuation of the timer? Have a look at my code. Show me where I am making mistake and what is the solution.

On the TimerTest.aspx.cs page, I have the following lines of code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;



public partial class Student_TimerTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    if (Session["end_t"] == null)
    {
        DateTime start_time = DateTime.Now;
        DateTime end_time = start_time.AddMinutes(3);
        Session["end_t"] = end_time;
    }
}

protected void timer1_tick(object sender, EventArgs e)
{
    DateTime dt = (DateTime)Session["end_t"];
    DateTime dt_curr = DateTime.Now;
    TimeSpan ts = dt - dt_curr;
    lblTimer.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
    if (ts.Minutes == 0)
    {
        timer1.Enabled = false;
        Response.Redirect("/Student/Result.aspx");
    }

}
}

On the TimerTest.aspx page, I have inserted the following lines of code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TimerTest.aspx.cs" Inherits="Student_TimerTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
        <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>
        </div>
   
       <div>
       <asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
       <ContentTemplate>
       <asp:Label ID="lblTimer" runat="server"></asp:Label>
       </ContentTemplate>
       <Triggers>
       <asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
       </Triggers>
       </asp:UpdatePanel>
</div>  
</form>
</body>
</html>

1 Answer 1

1

To say that the timer is still running is a little misleading. There isn't actually any code running after you quit debugging. However, you store the start time in session state which, by default, is stored in the browser. If you stop debugging and then start again, the application instance for your web app is restarted, but your browser preserves the session state. So your browser still has the old start time. Therefore, on page load, the line if (Session["end_t"] == null) results in false. There is a value there.

There are many options here, but one would be to always clear the value on page load if it's not a post back. If it is a post back, just check if there's a value in session already.

Sample code:

protected void Page_Load(object sender, EventArgs e)
{
    // If this is the initial page load (not a post back), 
    // or if there's not already an end time in Session, 
    // then set the end time
    if (!Page.IsPostBack || Session["end_t"] == null)
    {
        DateTime start_time = DateTime.Now;
        DateTime end_time = start_time.AddMinutes(3);
        Session["end_t"] = end_time;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. Now it is working working fine. Will you please show me how do I redirect to result page whenever trying to refresh the page or got to previous page by clicking on back arrow of the browser.
I'm sorry, I don't quite understand what you're asking. Furthermore, you may be best served by posting this follow-up question as another question and just linking to this one; it sounds like it's a different (though related) question.
I am trying to say is If the user refreshes the page then the page reload and the timer countdown restart, so I want to warn the user through an alert box if they really want to refresh the page and if they click ok then the page should be refreshed otherwise if they click cancel it won't be. How to make this type of alert box appear when the browser's refresh button is clicked in a way that is cross browser compatible?
It's difficult (and not really a good practice) to try to interrupt actions like Refresh. However, here's something you could do: on initial page load (not a post back), check if there's an end datetime in session already. If not, just start the timer. However, if there is, then display a message on the page asking if the user wants to keep the current timer going or start a new one. There could be two buttons on the page for those two options. Once the user clicks either button, the message and the buttons would need to be hidden, and then you either start a new timer...or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.