...

View Full Version : Prob w/ timer repeating a 0 per second for the hour.



RexxCrow
08-09-2006, 09:22 PM
Could I get some assistance with finishing this timer up, it seems to work fine, not sure if I have the timing set correct though, referring to the 1 sec timeouts and the 59 min and 59 sec hour change marker... I think that is the proper way to do it, but wanted to make sure.

I am having a prob with the hour though one the hour is reached it adds a 0 to the display per second passed, the prob is I have to wait 1 hour to pass before I can test my changes so it is very slow to figure it out on my own, was hoping somebody could point out what I am doing wrong on that part. i.e. the display would like something this just after the first hour: 00001:00:04, 00000000001:00:10, etc., until then it would like like: 59:59, and then when the hour comes I was wanting it to appear as: 1:00:01. THX.



window.onload=function(){
iT=new Date();
h=0;
document.getElementById('timer').innerHTML="00:00";
timer=setTimeout("update()",1000);
}
function update(){
clearTimeout(timer);
lapse=new Date();
timeComp=lapse.getTime()-iT.getTime();
lapse.setTime(timeComp);
m=lapse.getMinutes();
s=lapse.getSeconds();
m=pt(m);
s=pt(s);
if (m==59 && s==59){h++};
if (h>0){
document.getElementById('timer').innerHTML=h+":"+m+":"+s;
}else{
document.getElementById('timer').innerHTML=m+":"+s;
}
timer=setTimeout("update()",1000);
}
function pt(i){
if (i<10){i="0"+i};
return i
}

* Ok, just finished a new test and it appears to be working now, I stopped trying to put a leading 0 in the hour section, which I did not really want anyways, the hours looks better without that.

Mr J
08-09-2006, 09:52 PM
If you are simply timing something how about something like this?



<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

nowHours=0
nowMins=0
nowSecs=0

function tickingTheTock(){

if(nowSecs>59){
nowSecs=0
nowMins++
}

if(nowMins>59){
nowHours++
nowMins=0
}

showHours=chk(nowHours)
showMins=chk(nowMins)
showSecs=chk(nowSecs)

document.getElementById("timerdiv").innerHTML=showHours+" : "+showMins+" : "+showSecs


nowSecs++
tickTimer=setTimeout("tickingTheTock()",1000)

}

function chk(n){
if(n<10){
n="0"+n
}
return n
}

</script>

</HEAD>
<BODY onload="tickingTheTock()">

<div id="timerdiv"></div>

</BODY>
</HTML>

RexxCrow
08-09-2006, 10:55 PM
At first I was thinking of doing that, using a timeout, but was thinking that going off the clock would be more accurate (built in checks and balances?), or are they really just one in the same?

Mr J
08-10-2006, 12:05 AM
I would imagine that both are as acurate but with my example you are simply incrementing in 1 second intervals whereas in your example you get the date onload and then the current date every second, convert to milliseconds, deduct one from the other and then convert back, it just seems a lot more work to reach the same result.

RexxCrow
08-10-2006, 01:19 AM
Ok, that makes sense then.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum