Javascript Browser Window clearTimeout() Method stop a timer

Description

Javascript Browser Window clearTimeout() Method stop a timer

View in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="startCount()">Start count!</button>
<input type="text" id="txt">
<button onclick="stopCount()">Stop count!</button>
<script>
var c = 0;/*from   w w  w .  ja  v a  2  s .  c  om*/
var t;
var isTimerOn = 0;

function timedCount() {
  document.getElementById("txt").value = c;
  c = c + 1;
  t = setTimeout(timedCount, 1000);
}

function startCount() {
  if (!isTimerOn) {
    isTimerOn = 1;
    timedCount();
  }
}

function stopCount() {
  clearTimeout(t);
  isTimerOn = 0;
}
</script>

</body>
</html>



PreviousNext

Related