www.webdeveloper.com
Results 1 to 5 of 5

Thread: time format

  1. #1
    Join Date
    Feb 2010
    Posts
    49

    time format

    I'm trying to create a timer that exceeds 24 hours. So far I've used

    var time = new Date (new Date().toDateString() + ' ' + t);

    to take advantage of the formatting with time.toLocaleTimeString This thing can't take a t > 24 hours. Before I roll my own thingy, is there something far easier that I can use?

  2. #2
    Join Date
    Nov 2010
    Posts
    978
    you can cut the hours part out of the time string and keep your own hour count via a set interval, adding it as a variable before displaying it:

    Code:
    <html>
    <head>
    
    </head>
    <body onload="startClock()">
    
    <div id="timer"></div>
    <script>
    
    var secs=0;
    var hours=0;
    
    function startClock() {
    work_timer=setInterval(working,1000);
    hour_timer=setInterval(hourCounter,3600000);
    }
    
    function hourCounter() {
    hours++
    }
    
    function working() {
    secs+=1000;
    var d = new Date("Fri, 31 Dec 1999 00:00:00 GMT");
    d.setUTCMilliseconds(secs);
    counter=d.toUTCString().substring(19,25);
    document.getElementById('timer').innerHTML = hours+counter;
    }
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Feb 2010
    Posts
    49

    Thanks

    I guess that means nothing is already made and I have to set about parsing 26:23:54 for example, myself. Oh well. Oh and, I can never get setInterval to work. I know why its better than setTimeout, but on Chrome it acts funny for me. Not worth it to figure out why

  4. #4
    Join Date
    Nov 2010
    Posts
    978

    you're welcome

    the above works fine on my version of Chrome. I don't understand this: "I have to set about parsing 26:23:54 for example" - maybe if you could elaborate on what it was you are trying to do?

  5. #5
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by martingaleh View Post
    I guess that means nothing is already made and I have to set about parsing 26:23:54 for example, myself. Oh well. Oh and, I can never get setInterval to work. I know why its better than setTimeout, but on Chrome it acts funny for me. Not worth it to figure out why
    Your approach, if something does not work you tackle it again from a different perspective. If you go about programming like the brute at the vending machine trying to get it to re-vend a drink it didn't vend by pressing the drinks button you want and thumping the machine, expect nothing to happen.

    If your setIntervals are not working, what is it that you are doing that everyone else isn't?

    You will see things like setInterval(function(){ ... },1000); where the blind function is spread across several lines which is always prone to breaking things if your syntax is out be even the littlest thing like a missing ; or typo in a function call, property or method.

    You should never use a setTimeout for a repetitive task, thats what setInterval is for and people just fail to understand why which is simply the amount of lag the setTimeouts repetitive use introduces.

    These small amounts build in to large errors in accuracy very quickly, this is within the system and no amount of efficient coding will eliminate this lag which can be as much as 250ms per actuation on older slower machines and as far as I am aware, this is still the case, a baseline lag between interpretation to actual output exists, it may be smaller but it still exists.

    So giving up easily without investigating why your not able to get a setInterval to work when you say you can get a setTimeout to run, means you are obviously doing something additional to requirement or not doing enough, it depends.

    If you post an example, you may get guidance as to why or what and how to avoid it in future.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles