troubleshooting Question

javascript timer that counts up hours:minutes:seconds

Avatar of tentavarious
tentavarious asked on
JavaScript
2 Comments1 Solution932 ViewsLast Modified:
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();">
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros