How to make a counter in JavaScript

Web programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to make a counter in JavaScript

Post by Neo » Fri Mar 05, 2010 2:37 pm

This script will count in seconds.

JavaScript:

Code: Select all

<SCRIPT language="JavaScript">
<!--Timer in JavaScript
var timerform
speed=1000
function dotimer(){
     today=new Date()
     slutsec=today.getSeconds()
     slutmin=today.getMinutes()
     sluttim=today.getHours()
     sluta=(slutsec) + 60 * (slutmin) + 3600 * (sluttim)
     diff=sluta - starta
     tim=Math.floor(diff / 3600)
     min=Math.floor((diff / 3600 - tim) * 60)
     sek=Math.round((((diff / 3600 - tim) * 60) - min) * 60)

     document.timerform.timer.value=tim + ':'

     if(min<10) document.timerform.timer.value+='0'

     document.timerform.timer.value+=min + ':'

     if(sek<10) document.timerform.timer.value+='0'

     document.timerform.timer.value+=sek

     window.setTimeout("dotimer()",speed)
}

function Timer(){

     today=new Date()
     startsek=today.getSeconds()
     startmin=today.getMinutes()
     starttim=today.getHours()
     starta=(startsek) + 60 * (startmin) + 3600 * (starttim)
     document.write('<form name=timerform><input name=timer size=7')
     document.write('></form>')
     dotimer()
}

// end-->
</SCRIPT>
HTML code:

Code: Select all

<table width=420>
<tr>
<td align=center>

TIMER:
<SCRIPT language="JavaScript">
Timer() 
</SCRIPT>

</td>
</tr>
</table>
Post Reply

Return to “Web programming”