Going Further Lesson 27 of 30
JavaScript Timers — setTimeout and setInterval
Run code later with setTimeout and repeatedly with setInterval, stop timers with clearInterval, and build a live clock and a quiz countdown.
Some code shouldn't run right away. A message that disappears after 3 seconds, a clock that ticks every second, a quiz with a 60-second timer: these all use JavaScript timers.
setTimeout: run once, later
setTimeout(function, delayInMilliseconds);
1000 milliseconds = 1 second.
<button id="save">💾 Save</button>
<p id="toast" hidden style="background:#1d7a43;color:white;padding:8px 12px;border-radius:8px;display:inline-block">Saved!</p>
<script>
document.getElementById("save").addEventListener("click", () => {
const toast = document.getElementById("toast");
toast.hidden = false;
setTimeout(() => {
toast.hidden = true;
}, 2000);
});
</script>
setInterval: repeat every few seconds
<p id="clock" style="font:bold 36px monospace"></p>
<script>
function tick() {
const now = new Date();
const pad = (n) => String(n).padStart(2, "0");
document.getElementById("clock").textContent =
`${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
}
tick();
setInterval(tick, 1000);
</script>
We call tick() once immediately so the clock doesn't appear empty for the first second.
Stopping a timer
Both functions return an id. Pass it to clearTimeout or clearInterval to stop the timer:
<p id="time" style="font:bold 36px monospace">0.0</p>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button>
<script>
let tenths = 0;
let timerId = null;
const show = () => (document.getElementById("time").textContent = (tenths / 10).toFixed(1));
document.getElementById("start").addEventListener("click", () => {
if (timerId !== null) return; // already running
timerId = setInterval(() => { tenths++; show(); }, 100);
});
document.getElementById("stop").addEventListener("click", () => {
clearInterval(timerId);
timerId = null;
});
document.getElementById("reset").addEventListener("click", () => {
clearInterval(timerId);
timerId = null;
tenths = 0;
show();
});
</script>
A quiz countdown
<p id="left" style="font:bold 40px monospace">10</p>
<button id="go">Start quiz</button>
<p id="msg"></p>
<script>
document.getElementById("go").addEventListener("click", () => {
let seconds = 10;
const left = document.getElementById("left");
const msg = document.getElementById("msg");
msg.textContent = "Answer quickly! ⏳";
left.textContent = seconds;
const id = setInterval(() => {
seconds--;
left.textContent = seconds;
left.style.color = seconds <= 3 ? "crimson" : "inherit";
if (seconds === 0) {
clearInterval(id);
msg.textContent = "⏰ Time's up!";
}
}, 1000);
});
</script>
Timers don't pause your code
setTimeout doesn't make JavaScript "wait". It schedules the function and moves on immediately:
console.log("1");
setTimeout(() => console.log("2"), 0);
console.log("3");
Console
1 3 2
Even with a 0 ms delay, "2" prints last, because timer callbacks only run after the current code is finished. This is your first taste of asynchronous JavaScript: things that happen "later".
Timers aren't perfectly exact
A 1000 ms interval may fire after 1003 ms or 1016 ms, and browsers slow timers down in hidden tabs to save battery. For accurate clocks, compute the time from new Date() each tick, like our clock does, instead of counting ticks.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04