Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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.

  • #3
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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.

  • #5
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Ok, that makes sense then.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •