Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Count Down From a Deadline

In this video, you built a countdown for Sam’s recipe steps that keeps the right time even when the page is busy, and wrapped it in a factory that gives every step its own timer.

Key Points

  • Counting ticks drifts. A countdown that takes one second off on every setInterval() tick trusts each tick to come on time. While JavaScript is busy, the browser can’t interrupt it. When it’s free, one late tick runs and the rest are dropped, never made up.
  • Dropped ticks never come back. A late tick only makes the line stale for a moment, but every dropped tick costs the countdown a second for good. In the video, a ten-second countdown with a three-second busy stretch dropped two ticks and reached zero after about twelve seconds.
  • Background tabs make it worse. In a tab in the background, the browser runs timers even less often, so a tick-counting countdown falls further behind still.
  • Count down from a deadline. Note the deadline once, Date.now() plus the duration, and on every tick work out the time left as the deadline minus Date.now(). Math.ceil() rounds the milliseconds up to whole seconds, so a little under nine seconds shows as 9.
  • A busy page still delays a redraw, but every redraw is right. With the same busy stretch, the deadline version stopped at 8, jumped to 5 (four and a half seconds, rounded up) when the page was free, and in the video’s run reached zero right on the deadline.
  • The deadline can’t make a tick come on time. That was this run’s result. The countdown finishes on the first tick at or after the deadline, so a page that’s busy at that moment finishes late too. The deadline keeps the number right, but it can’t make a tick come on time. Once the time is up, the tick sets it to zero, so the line never shows a negative number.
  • The rule: keep the deadline, and work out what to show from Date.now(). The tick only decides when to redraw.
  • One timer per step. makeStepTimer() is a factory: each call adds its own line to the page and keeps its own time left, deadline and interval ID in its own box. pause() takes one last reading to bring the time left up to date, then clears the interval. start() sets the deadline to now plus the time left, so a restart picks up where the pause left off.
  • Start and pause guard themselves. start() checks the interval ID and the time left first, so it does nothing when its timer is already running or finished. pause() does nothing when nothing’s running. When the tick stops at zero, it sets the interval ID back to undefined, so the ID always tells you whether the timer is running.

The Code

Open Sam’s recipe box through Live Preview at http://127.0.0.1:3000/recipes/index.html, open the Console, and type each entry with Shift+Enter between its lines. The timers are toys that count seconds instead of minutes. The first entry sets up a stand-in for a busy page and a line for the countdown:

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

00:00 Sam’s status line now clears itself, and the recipe steps need timers next. In this video, you’ll build a countdown that keeps the right time, even when the page is busy.

00:11 Soft-boiled eggs boil for six minutes, so their step timer should show the time left, once a second. This toy timer runs for ten seconds instead.

00:21 Open Sam’s recipe box through Live Preview, at the address on screen, and type along in its Console, with Shift+Enter between lines. keepBusy stands in for a busy page: a loop that holds JavaScript for some milliseconds, like the hand-written wait in Schedule and Cancel Timers.

00:40 Then eggLine is a new paragraph at the end of the page, and showEggs writes the seconds left into it. A countdown that ticks once a second seems to need only a count, so most people try that first.

00:54 The second entry starts the busy page: two and a half seconds in, keepBusy runs for three seconds, like a slow redraw of a long recipe list.

01:05 Then secondsLeft starts at ten, and the page shows it. Every second, setInterval takes one off and shows the new number. At zero, it clears the interval and logs how long the countdown really took.

01:18 The page is busy for three of those ten seconds. Your turn: pause the video and predict when the line reaches zero.

01:31 Here’s the run, sped up. It reaches zero after twelve seconds, two seconds late.

01:39 Look at it on a timeline. The ticks come once a second: nine, eight. Then keepBusy holds JavaScript, and three ticks fall due while it runs. The browser can’t interrupt the loop.

01:52 When the loop ends, the interval runs one late tick and drops the rest. Two timeouts would both run late, but an interval is one timer, so it runs late once.

02:01 So the line says seven seconds left when only four and a half are left. This countdown counts ticks, not time. Two ticks were dropped, so it ends two seconds late, not three.

02:14 A background tab runs timers even less often. Counting ticks was a detour.

02:21 So stop counting. Note the deadline instead: the time now plus ten thousand milliseconds. On every tick, subtract the time now from the deadline. That gives the milliseconds left, however many ticks came.

02:35 Past the deadline, it sets them to zero, never negative. Math.ceil rounds them up to whole seconds, so a little under nine seconds still shows as nine.

02:46 The entry starts the same busy stretch, so press Enter. Ten, nine, eight, and the line stops again while the page is busy. When it’s free, the line jumps to five: four and a half seconds, rounded up.

03:01 This time, it reaches zero right on the deadline.

03:05 A busy page still delays a redraw, but every redraw shows the right number. So here’s the rule: keep the deadline, and work out what to show from the time now.

03:15 The tick only decides when to redraw.

03:20 A recipe can have several timed steps. That’s a job for a factory, as in Make a Function That Remembers. Here’s makeStepTimer as one entry: copy it from below the video.

03:32 Each call adds its own line to the page and keeps the time left, the deadline and the interval ID in its own box. Its tick works out the time left from the deadline, as before, and stops at zero.

03:46 The call returns two functions. Pause takes one last reading to bring the time left up to date, then clears the interval. Start sets the deadline to now plus the time left, so a restart picks up from there.

04:00 Then it starts the interval and shows the line.

04:04 Start does nothing if the timer’s already running or finished.

04:10 Make two timers, one to boil the eggs for ten seconds and one to fry the rice for four, and start both. Each line counts down on its own. Pause the eggs at seven, and their line holds while the rice reaches zero.

04:24 Start the eggs again, and they go on from seven.

04:28 Each step now keeps its own deadline, so its countdown stays right on a busy page. Next, in Exercise: Build an Egg Timer, you’ll give a page an egg timer and a toast timer, each with Start and Pause buttons.

Become a Member to join the conversation.