JavaScript Timers

Back to JS page

Table of Contents

The setTimeout() Function

The setTimeout() function runs a function after a specified number of milliseconds:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The setTimeout() Function</h4>
<p>Wait 2 seconds and a message will appear.</p>
<p id="demo"></p>

<script>
setTimeout(function() {
  document.getElementById("demo").innerHTML = "Hello after 2 seconds!";
}, 2000);
</script>

</body>
</html>

Example 1

Result View Example

Using an Anonymous Function

You can pass an anonymous function directly to setTimeout():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Using an Anonymous Function</h4>
<button onclick="startTimer()">Start Timer</button>
<p id="demo"></p>

<script>
function startTimer() {
  setTimeout(function() {
    document.getElementById("demo").innerHTML = "Timer finished!";
  }, 3000);
}
</script>

</body>
</html>

Example 2

Result View Example

Canceling a Timeout

The clearTimeout() function cancels a timeout set with setTimeout():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Canceling a Timeout</h4>
<button onclick="startTimer()">Start Timer</button>
<button onclick="cancelTimer()">Cancel Timer</button>
<p id="demo"></p>

<script>
let timeoutId;

function startTimer() {
  timeoutId = setTimeout(function() {
    document.getElementById("demo").innerHTML = "Timer finished!";
  }, 3000);
  document.getElementById("demo").innerHTML = "Timer started...";
}

function cancelTimer() {
  clearTimeout(timeoutId);
  document.getElementById("demo").innerHTML = "Timer canceled!";
}
</script>

</body>
</html>

Example 3

Result View Example

The setInterval() Function

The setInterval() function repeats a function at a specified interval:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The setInterval() Function</h4>
<p id="demo"></p>

<script>
let counter = 0;
setInterval(function() {
  counter++;
  document.getElementById("demo").innerHTML = "Counter: " + counter;
}, 1000);
</script>

</body>
</html>

Example 4

Result View Example

Canceling an Interval

The clearInterval() function stops an interval set with setInterval():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Canceling an Interval</h4>
<p id="demo"></p>
<button onclick="stopTimer()">Stop</button>

<script>
let counter = 0;
let intervalId = setInterval(function() {
  counter++;
  document.getElementById("demo").innerHTML = "Counter: " + counter;
}, 1000);

function stopTimer() {
  clearInterval(intervalId);
  document.getElementById("demo").innerHTML += " (stopped)";
}
</script>

</body>
</html>

Example 5

Result View Example

A Countdown Example

A countdown timer using setInterval():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Timers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>A Countdown Example</h4>
<p id="demo"></p>
<button onclick="startCountdown()">Start Countdown</button>

<script>
function startCountdown() {
  let count = 10;
  document.getElementById("demo").innerHTML = count;
  
  let intervalId = setInterval(function() {
    count--;
    document.getElementById("demo").innerHTML = count;
    if (count === 0) {
      clearInterval(intervalId);
      document.getElementById("demo").innerHTML = "Blast off!";
    }
  }, 1000);
}
</script>

</body>
</html>

Example 6

Result View Example

Document

Document in project

You can Download PDF file.

Reference