Schedule and Cancel Timers

So far, Sam’s egg timer runs a callback once, a little later. A real kitchen asks for more. When Sam takes the eggs off the heat early, the timer should stay quiet. Grandma Ines’s lentil soup simmers for 25 minutes and needs a stir every few minutes until it’s done.

In this lesson, you’ll hand a timer’s callback the values it needs, call a timer off before it fires, and repeat a reminder until you stop it. You’ll also be able to predict when a callback really runs, which isn’t always when its delay says. Start with the timer you know, and look at what setTimeout() hands back when you call it.

Schedule a Timer and Call It Off

The recipe box has to be able to find a timer it starts, or Sam can’t cancel it. So the first step is to see what starting a timer gives you.

In a new tab at about:blank, bring up the Console and keep this tab open for the whole lesson. The entry below defines announce(), which logs a recipe’s name and the step to do, and then starts a one-second timer for it. Press Shift+Enter between the lines:

Language: JavaScript
> function announce(recipe, step) {
…   console.log(`${recipe}: ${step}`);
… }
… setTimeout(announce, 1000, "Soft-boiled eggs", "drain");
1
Soft-boiled eggs: drain

The line arrives a second later, as you’d expect. In the entry’s last line, though, setTimeout() takes more than a callback and a delay. Any values after the delay are passed to the callback as its arguments when it runs, so the browser calls announce("Soft-boiled eggs", "drain") for you. An arrow function such as () => announce("Soft-boiled eggs", "drain") does the same job.

Watch out for parentheses, which call a function instead of naming it. If you write setTimeout(announce("Soft-boiled eggs", "drain"), 1000), then announce() runs and the line appears at once. setTimeout() then receives the return value of announce(), which is undefined, instead of a function.

The 1 under the entry is new too, and your code didn’t log it. It’s the Console’s echo of what setTimeout() returned: a number called the timer ID, which names this one timer. Your number may differ, but you can keep it and hand it to clearTimeout(), which cancels the timer if it hasn’t run yet.

Try it with a 30-second timer so you have time to change your mind. Store its ID in eggTimer, look at the ID, and then cancel the timer, each as its own entry. The first entry spans three lines, so press Shift+Enter between them:

Language: JavaScript
> const eggTimer = setTimeout(
…   announce, 30000, "Soft-boiled eggs", "drain"
… );
undefined
> eggTimer
2
> clearTimeout(eggTimer);
undefined

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.