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();">
JavaScript

Avatar of undefined
Last Comment
callrs

8/22/2022 - Mon
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
callrs

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck