JavaScript・Your syntax toolbox

Make things happen on a timer

"Show it after 3 seconds," "count up once every second"—not just clicks, time can be a trigger too. Let's learn the two timers.

You’ve learned “run when clicked.” But triggers aren’t only human actions—time can be a trigger too. “Show a message after 3 seconds,” “count a number down once every second”—let’s learn how to use the two timers.

Once, after ~ seconds: setTimeout

setTimeout(function () {
  console.log("3 seconds are up!");
}, 3000);

When you run it, the message appears in the console after waiting 3 seconds. The unit of time is milliseconds (one-thousandth of a second).

How you write itMeaning
10001 second
5000.5 seconds
30003 seconds

Repeat every ~ seconds: setInterval

setInterval repeats the same thing at a fixed interval. Showing a clock or auto-advancing a slideshow is this timer’s job.

setInterval(function () {
  console.log("Called once every second");
}, 1000);

Stop the repeat: clearInterval

If you leave setInterval alone, it repeats forever. To stop it, store the timer in a variable and pass it to clearInterval.

const timer = setInterval(function () {
  console.log("Still running");
}, 1000);

clearInterval(timer);   // ← this stops it

Think of it as “keep the timer’s ticket in timer, and when you want it to stop, show the ticket.”

Cancel a reservation: clearTimeout

As it happens, setTimeout has a matching way to stop it too. After you’ve reserved “do this in 3 seconds,” if you change your mind you can cancel it with clearTimeout.

const timer = setTimeout(function () {
  console.log("3 seconds are up!");
}, 3000);

clearTimeout(timer);   // ← you can cancel it as long as it hasn't run yet

The mechanism is the same ticket system as clearInterval. set reserves, clear cancels with the ticket—learning them as two pairs makes it easier to keep straight.

Let’s see it in action

Combining timers, variables, and if statements, you can build a countdown.

Once each second we drop rest by 1 and show it, and when it hits 0 we stop the repeat with clearInterval. “Decrease, check, stop”—all tools you’ve already learned.

Summary of this lesson

  1. setTimeout(thing to do, milliseconds) runs it once after ~ milliseconds; setInterval runs it over and over every ~ milliseconds
  2. The unit is milliseconds. 1 second = 1000 (three zeros)
  3. Stop a repeat with clearInterval(the timer's variable)

Try it

The “timers” you learned today are not used on Shima’s page. They’re tools for pages that move with time, like countdowns, clocks, and slideshows. Keep them in mind and use them when you need them.

FAQ

Can I set the time in seconds?
No. The only unit is milliseconds (one-thousandth of a second). For 1 second write 1000, for 3 seconds write 3000—take the number of seconds and add three zeros.
My setInterval won't stop. What should I do?
Store the result of setInterval in a variable, then pass that variable to clearInterval to stop it. If you forget to store it, you have no way to stop it, so make it a habit to put repeating timers in a variable from the start.
I pressed the button over and over and the countdown sped up. Why?
Each press starts a new timer in addition to the ones already running, so several timers count the number down at the same time. The standard fix is to stop the previous timer with clearInterval before you start.