Answered by:
Count Down timer for online quiz

Question
-
User1623409651 posted
Hi all,
I have to implement the time for online quiz i.e for 2 hours. Please suggest the way how i can implement this. During the question selection timer will continue.
Time : 01: 59 : 00
Regards,
Sunday, August 30, 2020 4:21 AM
Answers
-
User409696431 posted
Please take the time to look at the code and understand it. If you understand it you can easily modify it.
As the code comment indicates, it subtracts 5 seconds. Change the t5sec timespan used to be a 1 second timespan instead, if you want one second. (Since there is a postback for each Timer1_Tick, you may want to reconsider using 1 second if this will be a heavily used site, but that's up to you to determine.)
Next, look at the line that outputs the counter to the label. Modify it to use the format you want to display, replacing the existing text with ":" and adding the hours part.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 2, 2020 12:37 PM
All replies
-
User409696431 posted
You haven't told us what kind of application this is.
Web Forms? See https://forums.asp.net/t/2170215.aspx?count+down+timer+in+asp+net+using+ajax and adjust the timespan from 5 minutes to 2 hours, adjust the output in the label to include the hours and separate by : instead of words.
MVC full framework? CORE? Different answers would be needed.
Sunday, August 30, 2020 2:19 PM -
User1623409651 posted
You haven't told us what kind of application this is.
I am using Webform and it is online quiz application therefore i need application level countdown timer
Regards,
Monday, August 31, 2020 6:09 AM -
User-939850651 posted
Hi Rameezwaheed,
You could use the Ajax Timer control to implement a countdown timer.
Of course, it has some shortcomings, it will post request each seconds. Too frequently request to server is not a good idea and it will create much more overhead on the server. So I recommend you to use JavaScript to achieve.
Please refer to these case:
How to show Count down time on online exam web application in asp.net c#
how to create a countdown timer using C# and ASP.NET AJAX ?
Best regards,
Xudong Peng
Monday, August 31, 2020 7:44 AM -
User409696431 posted
If you are using webforms, see the link I gave in my first reply.
Monday, August 31, 2020 11:26 AM -
User1623409651 posted
KathyW
If you are using webforms, see the link I gave in my first reply.
Thanks for reply.
<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" Font-Bold="True" Font-Names="Arial" Font-Size="X-Large" ForeColor="#6600CC"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" /> </Triggers> </asp:UpdatePanel> </div> </form>
protected void Page_Load(object sender, EventArgs e) { if (!SM1.IsInAsyncPostBack) Session["timeout"] = DateTime.Now.AddMinutes(90).ToString(); } protected void timer1_tick(object sender, EventArgs e) { if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString()))) { lblTimer.Text = string.Format("Time Left: 00:{0}:{1}", ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString()); } else { timer1.Enabled = true; Response.Redirect("Logout.aspx"); } }
above code runs successfuly and show the timer on page but i need if i add the two hour then it must display the
1: 59 : 01
One hour and 59 minutes and One second initially when page loads
Regards,
Regards.
Tuesday, September 1, 2020 8:10 AM -
User409696431 posted
If you want to use the code I showed you, please read the link again and follow it. It is quite different from what you did, and shows the starting countdown on Page_Load. (It also has no need for DateTime.Now(). You don't care what time it is. You want to count down 2 hours, no matter what time it is, which is why it uses a timespan.)
Tuesday, September 1, 2020 11:44 AM -
User1623409651 posted
Thanks for the reply,
I have used your code which is given in your first reply link. it shows time every five second i need the time in countinous format like
01:59: 01
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["CountdownTimer"] == null) { Session["CountdownTimer"] = new TimeSpan(0, 0, 0); } TimeSpan current = (TimeSpan)Session["CountdownTimer"]; Label1.Text = current.ToString("%m") + " minutes and " + current.ToString("%s") + " seconds"; } } protected void Timer1_Tick(object sender, EventArgs e) { TimeSpan ts5sec = new TimeSpan(0, 0, 5); // 5 seconds TimeSpan ts = (TimeSpan)Session["CountdownTimer"]; // current remaining time from Session TimeSpan current = ts - ts5sec; // Subtract 5 seconds Label1.Text = current.ToString("%m") + " minutes and " + current.ToString("%s") + " seconds"; Session["CountdownTimer"] = current; // put new remaining time in Session //Add your code here to test for remaining time = 0 and clear the session, stop the timer, and do whatever you need to do after 5 minutes }
Regards,
Wednesday, September 2, 2020 5:49 AM -
User409696431 posted
Please take the time to look at the code and understand it. If you understand it you can easily modify it.
As the code comment indicates, it subtracts 5 seconds. Change the t5sec timespan used to be a 1 second timespan instead, if you want one second. (Since there is a postback for each Timer1_Tick, you may want to reconsider using 1 second if this will be a heavily used site, but that's up to you to determine.)
Next, look at the line that outputs the counter to the label. Modify it to use the format you want to display, replacing the existing text with ":" and adding the hours part.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 2, 2020 12:37 PM