Changing seconds to hh:mm:ss format

Discussion in 'JavaScript' started by handyrandy, Jun 16, 2010.

  1. #1
    I have a consecutive countdown currently using only seconds. I would like the output to show hours minutes and seconds (ex 2:30:00). The page is at riccraig.us/countdown.html. Here is some of the code:

    
    var a=900;
    var b=600;
    var c=1200;
    var d=7200;
    var e=600;
    var f=1800;
    
    var t1;
    var t2;
    var t3;
    
    var A=0;
    var B=0;
    var C=0;
    var D=0;
    var E=0;
    var F=0;
    
    function timedCountdown1()
    {	
    	document.getElementById('btn').style.visibility= "hidden";
    	t1=setTimeout("timedCountdown1()",1000);
    	document.getElementById("timeA").innerHTML=a;
    	a=a-1;
    	if(a==-1){a=0; clearTimeout(t1); 
    	timedCountdown2();}
    }
    
    function timedCountdown2()
    {	
    	t2=setTimeout("timedCountdown2()",1000);
    	document.getElementById('timeB').innerHTML=b;
    	b=b-1;
    	if(b==-1){b=0; clearTimeout(t2);
    	timedCountdown3();}
    }
    
    Code (markup):
    Countdowns 3 through 6 are the same as 1 and 2. The initial html shows the desierd values. But when the timer starts it only shows the total seconds. I realize why it is showing the total number of seconds. However, I don't know how to get it to the 00:00:00 format.

    Please help a newbie out. This is my second run with javascript.
     
    handyrandy, Jun 16, 2010 IP
  2. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    javascript lacks of format functions and we use additional source codes like sprintf() to even that out. But you just need a simple modification, so use the following source:

    
    <html>
    <body>
    
    <script type="text/javascript">
    
    function lead0(s) {
      if (s.length == 1) {
        s = '0' + s;
      }
      return s;
    }
    
    document.write(lead0('11') + ':' + lead0('5') + ':' + lead0('8'));
    
    </script>
    </body>
    </html>
    
    
    Code (markup):
     
    flexdex, Jun 17, 2010 IP