Count down timer in asp.net
We can display the count down by using timer control or by
using the java script.
Timer control will repeatedly call the post back on every
tick, which will decrease the performance of the site. It’s better to use the
java script.
Following is the code for implementing the Count Down:-
Test.aspx:
<asp:Label ID="lblTimer"
runat="server"></asp:Label>
Write this line in body, if it is simple one page site.
If you have master page the write above code in the asp:Content.
Script:-
On the same page write
following script:-
This script will update
the time and on label and calculate the time in Hours, Minutes, and Seconds.
<script type="text/javascript"
language="javascript">
var timeout = "<%= Session.Timeout * 60 * 1000 %>";
var lbltimer =
document.getElementById("<%=
lblTimer.ClientID %>");
debugger;
var timer = setInterval(function() {
timeout -= 1000;
lbltimer.innerHTML = "Your
session will time out in " + time(timeout) + " minutes.";
if (timeout == 0) {
clearInterval(timer);
alert(' Your time over
');
window.location.replace("../Login.aspx");
}
}, 1000);
function too(x) {
return ((x > 9) ? "" : "0")
+ x;
}
function time(ms) {
var t = "";
var sec = Math.floor(ms /
1000);
ms = ms % 1000;
var min = Math.floor(sec
/ 60);
sec = sec % 60;
t = too(sec);
var hr = Math.floor(min /
60);
min = min % 60;
t = too(min) + ":"
+ t;
return t;
}
</script>
Here we are redirect the
page to “login.aspx” page when the time over and on ever y load the timer will
reinitialize. As we are setting time on ever new session in Global.asax page.
Global.asax
void
Session_Start(object sender, EventArgs e)
{
Session.Timeout
= 45;
}
We are setting the session
for 45 minutes after 45 minutes page will redirect to the login.apx page.
I have create this for online test, may cause the lose of data but if u save the item on every click event than no question will arises lose of data.
Comments
Post a Comment