Link to home
Create AccountLog in
Avatar of tentavarious
tentavarious

asked on

javascript timer that counts up hours:minutes:seconds

Hello experts I am trying to create a timer that will count up in hours minutes seconds.  
In this format 00:00:00.  I have 3 labels with ids lblsecond, lblminute, lblhour.  I cant seem to get this to work.

I have a timer that fires when the page loads and I make use of ajax to call serverside code and connect to a database.  That part you dont have to worry about because it works.  The function ServerSideAdd is a server function that connects to the database.  What I want to do is monitor the value that ServerSideAdd retrieves and if its equal to 1 start another timer that increments the seconds and minutes and so on.  How can I make this work?  Below i have some code which retrieves the value every second to see if it has changed.  The code below doesnt work but it gives you an idea of what I need, can someone modify this to work?

<script language = javascript>
var el=10
var sec=1
var min=0
var hour=0
var times = ServerSideAdd();
function starttimer(){
el--  
if (el>=0){
     setTimeout("starttimer()",1000)
     times = ServerSideAdd();
    if (times==1){
   sec ++
      document.getElementById('lblsecond').innerHTML = sec;
      if (sec == 60){
         sec = 0
         min ++
         document.getElementById('lblminute').innerHTML = min;
       }
     if (min==60){
      min = 0
      hour ++
      document.getElementById('lblhour').innerHTML = hour;
     }
   }
   else{
   document.getElementById('lblsecond').innerHTML = 00;
   document.getElementById('lblminute').innerHTML = 00;
   document.getElementById('lblhour').innerHTML = 00;
   }
  }
else{
     el=10;
     starttimer();
     }
}

</script>


      </HEAD>
      <body onload="starttimer();">
Avatar of callrs
callrs

Try this demo:
<script language = javascript>
var el=10
var sec=1
var min=0
var hour=0

var times=Math.floor(Math.random()*10);

function starttimer(){
el--  
if (el>=0){
      setTimeout("starttimer()",1000)
      times=Math.floor(Math.random()*10);
      if (times!=1){
            sec ++
            document.getElementById('lblsecond').innerHTML = sec;
            if (sec == 60){
                  sec = 0
                  min ++
                  document.getElementById('lblminute').innerHTML = min;
                  }
            if (min==60){
                  min = 0
                  hour ++
                  document.getElementById('lblhour').innerHTML = hour;
                  }
            }
      else{
            document.getElementById('lblsecond').innerHTML = 00;
            document.getElementById('lblminute').innerHTML = 00;
            document.getElementById('lblhour').innerHTML = 00;
            }
      }
else{
     el=10;
     starttimer();
     }
}

</script>

     </HEAD>
     <body onload="starttimer();">

<div id="lblhour">
</div>
<div id="lblminute">
</div>
<div id="lblsecond">
</div>
ASKER CERTIFIED SOLUTION
Avatar of callrs
callrs

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account