Documentation

Student-friendly HTML, CSS, and JavaScript reference with examples

Sections

setTimeout

setTimeout() runs a function once after a delay.

setTimeout(function() {
  console.log("Done!");
}, 3000);

This prints “Done!” after 3 seconds (3000 milliseconds).

Cancelling it:

Save the return value and pass it to clearTimeout() to cancel before it runs:

const timer = setTimeout(function() {
  console.log("This won't run");
}, 5000);

clearTimeout(timer);

Common use: showing a message briefly:

const message = document.querySelector(".message");
message.textContent = "Saved!";

setTimeout(function() {
  message.textContent = "";
}, 2000);