title: javascript timer
body: <p>I am trying to make the following javascript timer perform two functions with a click of one button only  - click once the timer starts; click again; it stops. click a third time it starts again, and so forth. What am I doing wrong in here? Thank you very much in advance.</p>
  
  <pre><code>    &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          var c=0;
          var t;
          var timer_is_on=0;
  
          function timedCount()
          {
              document.getElementById('txt').value=c;
              c=c+1;
              t=setTimeout("timedCount()",1000);
          }
  
          function doTimer()
          {
              if (!timer_is_on)
              {
                  timer_is_on=1;
                  timedCount();
              }
          }
  
          function stopCount()
          {
              clearTimeout(t);
              timer_is_on=0;
          }
          function both(){
              doTimer();
              stopCount();
          }
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form&gt;
          &lt;input type="button" value="Start count!" onclick="doTimer" /&gt;
          &lt;input type="text" id="txt" /&gt;
          &lt;input type="button" value="Stop count!" onclick="stopCount()" /&gt;
      &lt;/form&gt;
  &lt;p&gt;
  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. Click on the "Start count!" button to start the timer again.
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>