...

View Full Version : stopwatch



sharky0597
12-16-2010, 05:30 AM
hi everyone, I am currently working on a stopwatch and I can't for the life of me get it inside this textbox.. Can anyone help with it? no matter what I seem to try it always goes outside of it...




<html>
<head>
<title> stopwatch </title>
<script type="text/javascript">

var setT, h, m, s;

function startWatch(b){
var ticker=document.getElementById('ticker');
var HH=h<10?'0'+h:h;
var MM=m<10?'0'+m:m;
var SS=s<10?'0'+s:s;
ticker.innerHTML = HH+':'+MM+':'+SS;
if(!b){stopWatch();return}
s++;
if(s==60){s=0;m++}
if(m==60){m=0;h++}
setT=setTimeout(function(){startWatch(b)},50)
}

function stopWatch(){
clearTimeout(setT);
}

function resetWatch(){
h=0; m=0; s=0;
startWatch(false)
}
onload=resetWatch

</script>
</head>

<body>

<form action="">

<input type="button" value="start clock" onclick="startWatch(true)">
<input type="button" value="stop clock" onclick="stopWatch()">
<input type="button" value="reset clock" onclick="resetWatch()">
<br>
<br>
<span id="ticker"></span>

<BR>
<INPUT type="text" id="ticker">
</form>
</body>
</html>

Logic Ali
12-16-2010, 06:03 AM
You have two elements with the same ID.
You need to write to the .value property of the text box not innerHTML.

Old Pedant
12-16-2010, 07:03 AM
LOL! How funny!!!!

The setTimeout is 50. That's 50 milliseconds.

And EVERY TIME startWatch is called, it increments the time by ONE SECOND!!!

So the "stop watch" is runing 20 times too fast!!!! (20 times 50ms == 1000ms == 1second).

That's not a "StopWatch". That's a "FastWatch".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum