Seeking javascript timer example that has start & stop buttons

I'm looking for an example of a javascript timer that displays both hours and seconds, and that  increments and displays in realtime.  The example needs to have start / stop buttons on it which start and stop the timer when clicked.  

Thanks,
- Yvan
egoselfaxisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SadafRasheedCommented:
0
HonorGodSoftware EngineerCommented:
Like this?
<html>
<body>
<script type='text/javascript'>
  var timerID  = 0;
  var started  = null;
  var target   = null;
  var remnant  = 0;
 
  function field( id ) {
    var ele = document.getElementById( id );
    if ( !ele ) {
      alert( 'Specified element not found. id="' + id + '"' );
    }
    return ele;
  }
  
  function UpdateTimer() {
    function D2( val ) {
      return ( ( val < 10 ) ? '0' : '' ) + val
    }
    
    var S = Math.floor( ( ( new Date() ).getTime() - started.getTime() ) / 1000 );
    var H = Math.floor( S / 3600 );
    S -= H * 3600;
    var M = Math.floor( S / 60 );
    S -= M * 60;
    
//  if ( timer.getSeconds() % 60 == 0 ) {
      target.value = D2( H ) + ':' + D2( M ) + ':' + D2( S );
//  }
 
   // Check once per second
    timerID = setTimeout( 'UpdateTimer()', 1000 );
  }
 
  function enable( id ) {
    var button = field( id );
    if ( button ) {
      button.removeAttribute( 'disabled' );
    }
  }
 
  function disable( id ) {
    var button = field( id );
    if ( button ) {
      button.disabled = 'disabled';
    }
  }
 
  function Start() {
    if ( target ) {
      started  = new Date();
      started.setTime( started.getTime() - ( remnant * 1000 ) );
      timerID  = setTimeout( 'UpdateTimer()', 1000 ) ;
      disable( 'start' );
      enable( 'stop' );
    }
  }
 
  function Stop() {
    clearTimeout( timerID );
    timerID  = 0;
    enable( 'start' );
    disable( 'stop' );
    var HMS = target.value.split( ':' );
    remnant = parseInt( HMS[ 0 ], 10 ) * 3600 + parseInt( HMS[ 1 ], 10 ) * 60 + parseInt( HMS[ 2 ], 10 );
  }
 
  function Reset() {
    target = field( 'Time' );
    if ( target ) {
      target.value = '00:00:00';
      enable( 'start' );
    } else {
      disable( 'start' );
    }
    disable( 'stop' );
  }
  
  function validate( obj ) {
    if ( !/^\d{2}:\d{2}\d{2}$/.test( obj.value ) ) {
      alert( 'Unexpected format: HH:MM:SS' );
      obj.value = '00:00:00';
    }
  }
</script>
<body onload='Reset()' onunload='Stop()'>
  <form name='myTimer'>  
    <input type='text'   id='Time'  size='5' onchange='validate(this.value)'>
    <input type='button' id='start' value='Start' onclick='Start()'>
    <input type='button' id='stop'  value='Stop'  onclick='Stop()'>
  </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
egoselfaxisAuthor Commented:
Perfect - thanks!

- Yvan
0
HonorGodSoftware EngineerCommented:
You are very welcome.

Thanks for the grade & points.

Good luck & have a great day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.