Resetting my Stop watch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajneesh Chellapilla
    New Member
    • Oct 2008
    • 12

    Resetting my Stop watch

    I wrote this program. Its kinda of strange when I make a reset function reset(){c=0} its doest reset the setTimeout. However if I directly pass c=0 to the onclick button it does reset the timer. What is the logic of this?
    Here is the program:


    [HTML]<html>
    <head>
    <script type="text/javascript">
    var c=0;
    var t;
    function timedCount()
    {
    document.getEle mentById('txt') .value=c;
    c=c+1;
    t=setTimeout("t imedCount()",10 00);
    }

    function stopCount()
    {
    clearTimeout(t) ;
    }

    function reset()
    {
    c=0;

    }

    </script>
    </head>

    <body>
    <form>
    <input type="button" value="Start count!" onClick="timedC ount()">
    <input type="text" id="txt">
    <input type="button" value="Stop count!" onClick="stopCo unt()">

    //Problem Area---------------------------------------------------------------------

    <input type="button" value="reset!" onClick="reset( );">// if I pass "c=0" it works
    //yet if I call the reset function which sets c=0 it doesnt work Why?

    </form>

    //-----------------------------------------------------------------------------------------------------

    <p>
    Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting.
    </p>
    </body>

    </html>[/HTML]
    Last edited by gits; Oct 23 '08, 08:55 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    See this similar recent thread.

    Comment

    • Rajneesh Chellapilla
      New Member
      • Oct 2008
      • 12

      #3
      My question isnt a coding question as much as a logic question. Basically I want to know why: does setting c=0 in the onclick function produce a different result than writing a function reset() {c=0} and inserting the reset() function into onclick.

      Setting c=0 in onlclick makes it work button works. However when I put the function reset() it just clears the field but does not restart the timer. Instead the timer continues where it left off. Here is the exact code, scroll to near the bottom to see what I am talking about I put comment lines.

      [HTML]<html>
      <head>
      <script type="text/javascript">
      var c=0;

      var d=15001;
      var e=19182;
      var t;



      function timedCount()
      {
      String.fromChar Code(c)

      document.getEle mentById('txt') .value=String.f romCharCode(c);
      document.getEle mentById('text' ).value=c;
      c=(c+1);
      d=(d+1);
      e=(e+1);
      t=setTimeout("t imedCount()",1) ;

      }



      function stopCount()
      {
      clearTimeout(t) ;
      }




      function reset()
      {
      c=0;

      }


      </script>












      <style type="text/css">

      .style1 {
      font-family: ;
      font-size: 60px;
      color: rgb(0,100,175);
      background-color: #FFFFCC;
      padding: 2px;
      height: 80px;
      width: 100px;
      border: 1px solid #7F9DB9;
      }

      .style2 {
      font-family: ;
      font-size: 60px;
      color: rgb(0,100,175);
      background-color: #FFFFCC;
      padding: 2px;
      height: 80px;
      width: 100px;
      border: 1px solid #7F9DB9;
      }




      </style>




      </head>

      <body>
      For God who inspires
      <form>
      <input type="button" value="Start Count" onClick="timedC ount()">


      <input type="text" class="style1" id='txt'><br></br>

      <input type="button" value="Stop count!" onClick="stopCo unt()">
      <input type="button" value="reset" onClick="c=0;">//this works
      //however this does not:
      //<input type="button" value="reset" onClick="reset( )"> does Not work
      //my qestion is why?
      <input type="text" class="style2" id='text'>



      </form>

      <br></br>
      <br></br>
      <br></br>
      <p>Click on the button above. The the imput will start counting throgh various unicode characters.</p>
      </body>

      </html>[/HTML]
      Last edited by acoder; Oct 24 '08, 04:44 PM. Reason: Added [code] tags

      Comment

      • zaphod42
        New Member
        • Oct 2008
        • 55

        #4
        reset it a reserved keyword:) try "resetC()" instead, I just tested it and it worked fine:)

        Comment

        • Rajneesh Chellapilla
          New Member
          • Oct 2008
          • 12

          #5
          Thank you for your help. I will try it my self. I am new to Javascript and programming and I appreciate you taking the time to help me.

          Comment

          Working...