Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Javascript Timer

  Asked By: Pamela    Date: Dec 12    Category: Asp.net    Views: 1257
  

I have put this timer javascript into my code wanting it to start counting and display the count on my page. I can figure out how to display the timer in my form here is my code thus far.


Sub StartTimer()
' Timer JavaScript
Dim sScript as String
sScript = "<SCRIPT LANGUAGE=JavaScript>"
sScript += "var timerID = null"
sScript += "var timerRunning = false"
sScript += "var startDate"
sScript += "var startSecs"
sScript += "function stopclock()"
sScript += "{"
sScript += "if(timerRunning)"
sScript += "clearTimeout(timerID)"
sScript += "timerRunning = false"
sScript += "}"
sScript += "function startclock()"
sScript += "{"
sScript += "startDate = new Date()"
sScript += "startSecs = (startDate.getHours()*60*60) + (startDate.getMinutes()*60)"
sScript += "+ startDate.getSeconds()"
sScript += "stopclock()"
sScript += "showtime()"
sScript += "}"
sScript += "function showtime()"
sScript += "{"
sScript += "// this doesn't work correctly at midnight..."
sScript += "var now = new Date()"
sScript += "var nowSecs = (now.getHours()*60*60) + (now.getMinutes()*60) + now.getSeconds()"
sScript += "var elapsedSecs = nowSecs - startSecs;"
sScript += "var hours = Math.floor( elapsedSecs / 3600 )"
sScript += "elapsedSecs = elapsedSecs - (hours*3600)"
sScript += "var minutes = Math.floor( elapsedSecs / 60 )"
sScript += "elapsedSecs = elapsedSecs - (minutes*60)"
sScript += "var seconds = elapsedSecs"
sScript += "var timeValue = '' + hours"
sScript += "timeValue += ((minutes < 10) ? ':0' : ':') + minutes"
sScript += "timeValue += ((seconds < 10) ? ':0' : ':') + seconds"
sScript += "// Update display"
sScript += "Txt_Timer.text = timeValue"
sScript += "timerID = setTimeout('showtime()',1000)"
sScript += "timerRunning = true"
sScript += "}<"
sScript += "/SCRIPT>"

If ( Not IsClientScriptBlockRegistered("TimerScript")) Then
RegisterClientScriptBlock("TimerScript", sScript)
End If
End Sub

<form runat="server" >
<table>
<tr>
<td>Timer <asp:textbox ID="Txt_Timer" runat="server" /></td>
</tr>
<tr>
<td>First Name: <asp:textbox ID="Txt_Name" runat="server" TextMode="SingleLine" /></td>
</tr>
<tr>
<td>Next Name: <asp:textbox ID="Txt_NextName" runat="server" TextMode="SingleLine" /></td>
</tr>
</table>
</form>

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jimmy Abp     Answered On: Dec 12

Something along the lines of:

sScript += "document.form[0]." + Txt_Timer.ClientID + ".value = timeValue"

Typed that in the mail editor...might need tweaking.

 
Didn't find what you were looking for? Find more on Javascript Timer Or get search suggestion and latest updates.




Tagged: