JavaScript example for timer

<html>
<head>
<script type=”text/javascript”>
var i=0;
function addnum()
{
//document.write(i + ‘<br/>’ );
document.getElementById(‘txt’).innerHTML=”Second: ” + i;
i++;
window.setTimeout(‘addnum()’ ,1000);
}

var mydate1 = new Date();
var timerFlag = 0;
mydate1.setMinutes(0,0,0);
function timer()
{

m = mydate1.getMinutes();
s = mydate1.getSeconds();
ms = mydate1.getMilliseconds();
ms1 = ms/10;
if (m<10) m = ‘0’+m;
if (s<10) s = ‘0’+s;
if (ms1<10) ms1 = ‘0’+ms1;

document.getElementById(‘txt1’).innerHTML=m + “:” + s + “:” + ms1;
if (timerFlag==0) return;

mydate1.setMilliseconds(mydate1.getMilliseconds()+10);
window.setTimeout(“timer()”,1)

}
function startTimer()
{
timerFlag = 1;
document.getElementById(‘timerFlag’).innerHTML=”timerFlag: ” + timerFlag;
timer();
}

function stopTimer()
{
timerFlag = 0;
document.getElementById(‘timerFlag’).innerHTML=”timerFlag: ” + timerFlag;
//timer();
}

function resetTimer()
{
timerFlag = 0;
document.getElementById(‘timerFlag’).innerHTML=”timerFlag: ” + timerFlag;
mydate1.setMinutes(0,0,0);
timer();
}

</script>

</head>

<body>

<div id=”txt”>
<script type=”text/javascript”>
addnum();
</script>
</div>

<br />
<input type=”button” value=”Start Timer” onclick=”startTimer()”/>
<input type=”button” value=”Stop Timer” onclick=”stopTimer()”/>
<input type=”button” value=”Stop Timer” onclick=”resetTimer()”/>
<br />
<div id=”txt1″>
<!–
<script type=”text/javascript”>
timer()
</script>
–>
</div>

<div id=’timerFlag’>
<script type=”text/javascript”>
resetTimer()
</script>
</div>

</body>
</html>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: