After updating this article on 06 July 2009, I see that there is someone who thinks that I don’t know that there are other solutions to this task today. As Stephen Inglish pointed out in a message, one of them is to use the AJAX timer module, and as fare as I know, there may be other solutions to.
After the article was published in 2005, there have been some comments from isikiss and obed21, members of the community, which have improved the code. The reason why I’m updating this article, even though it is outdated, is that there are still people who read this article and I think they deserve an updated code.
Using the code
Start a new ASP.NET Web Application. Call it whatever you want, maybe TimerTest, and do the following:
var timerID = null
var timerRunning = false
var delay = 1000
secs = 5
timerRunning = false
secs = secs - 1
timerRunning = true
timerID = self.setTimeout("StartTheTimer()", delay)
Body have to look something like this. The most important thing is to add
onload="InitializeTimer()" inside the
<form id="form1" runat="server">
The time is: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
In the code-behind, you can paste in this just to see something happening.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Label1.Text = Now.ToString("hh:mm:ss")
Leave it like this. The only thing it’s doing is that it posts back to the server, and in this example, it’s refreshing the time every 5 seconds.
And that’s all.
- 06 July 2009: The article was made when I was using Visual studio 2003. There have also been some comments by isikiss and obed21, members of the community, that have improved the code. I have implemented the improvements they have suggested and used Visual Studio 2008 to test it.