Search code examples
javascriptstopwatch

to implement reset in stopwatch


I have a javascript stopwatch and i have start and stop but i like to add another button reset pls help me to solve this issue.I have placed the javascript below.

<script type="text/javascript">
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;
}
</script>
</head>

<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>

Solution

  • HTML:

    <input type="button" value="reset count!" onClick="resetCount()">
    

    JS:

    function resetCount() {
         document.getElementById('txt').value =0;
        c =0;
    }
    

    JSFiddle