Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: stopwatch

  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    stopwatch

    hi everyone, I am currently working on a stopwatch and I can't for the life of me get it inside this textbox.. Can anyone help with it? no matter what I seem to try it always goes outside of it...

    Code:
    <html>
    <head>
    <title> stopwatch </title>
    <script type="text/javascript">
    
    var setT, h, m, s;
    
    function startWatch(b){
    var ticker=document.getElementById('ticker');
    var HH=h<10?'0'+h:h;
    var MM=m<10?'0'+m:m;
    var SS=s<10?'0'+s:s;
    ticker.innerHTML = HH+':'+MM+':'+SS;
    if(!b){stopWatch();return}
    s++;
    if(s==60){s=0;m++}
    if(m==60){m=0;h++}
    setT=setTimeout(function(){startWatch(b)},50)
    }
    
    function stopWatch(){
    clearTimeout(setT);
    }
    
    function resetWatch(){
    h=0; m=0; s=0;
    startWatch(false)
    }
    onload=resetWatch
    
    </script>
    </head>
    
    <body>
    
    <form action="">
    
    <input type="button" value="start clock" onclick="startWatch(true)">
    <input type="button" value="stop clock" onclick="stopWatch()">
    <input type="button" value="reset clock" onclick="resetWatch()">
    <br>
    <br>
    <span id="ticker"></span>
    
    <BR>
    <INPUT type="text" id="ticker">
    </form>
    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You have two elements with the same ID.
    You need to write to the .value property of the text box not innerHTML.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    LOL! How funny!!!!

    The setTimeout is 50. That's 50 milliseconds.

    And EVERY TIME startWatch is called, it increments the time by ONE SECOND!!!

    So the "stop watch" is runing 20 times too fast!!!! (20 times 50ms == 1000ms == 1second).

    That's not a "StopWatch". That's a "FastWatch".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •