Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-09-2006, 08:22 PM   PM User | #1
RexxCrow
Regular Coder

 
RexxCrow's Avatar
 
Join Date: Jul 2006
Location: California
Posts: 275
Thanks: 6
Thanked 2 Times in 2 Posts
RexxCrow has a little shameless behaviour in the past
Prob w/ timer repeating a 0 per second for the hour.

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.


Code:
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.

Last edited by RexxCrow; 08-09-2006 at 10:08 PM..
RexxCrow is offline   Reply With Quote
Old 08-09-2006, 08:52 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
If you are simply timing something how about something like this?

PHP Code:
<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> 
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 08-09-2006, 09:55 PM   PM User | #3
RexxCrow
Regular Coder

 
RexxCrow's Avatar
 
Join Date: Jul 2006
Location: California
Posts: 275
Thanks: 6
Thanked 2 Times in 2 Posts
RexxCrow has a little shameless behaviour in the past
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?
RexxCrow is offline   Reply With Quote
Old 08-09-2006, 11:05 PM   PM User | #4
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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.
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 08-10-2006, 12:19 AM   PM User | #5
RexxCrow
Regular Coder

 
RexxCrow's Avatar
 
Join Date: Jul 2006
Location: California
Posts: 275
Thanks: 6
Thanked 2 Times in 2 Posts
RexxCrow has a little shameless behaviour in the past
Ok, that makes sense then.
RexxCrow is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:59 AM.


Advertisement
Log in to turn off these ads.