Javascript Browser Window setTimeout() Method count

Description

Javascript Browser Window setTimeout() Method count

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;/* w  ww . java2 s. co m*/
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