Loading video player…

Run Code Later Without Freezing

In this video, you started an egg timer for Sam’s recipe box and saw that setTimeout() doesn’t pause your code. The video’s Soft-boiled eggs recipe is a starter the box gains later in the course, so your box doesn’t show it yet.

Key Points

  • setTimeout() takes a callback and a delay in milliseconds. It calls the callback once the delay is up and JavaScript has nothing else running. Busy code or a tab in the background can make that later than the delay. A two-second toy timer uses 2000. The callback works like the one you pass to addEventListener(): you hand it over, and the browser calls it later.
  • It doesn’t pause anything. If you read it as a sleep command, you’d expect the code after it to wait too. Instead, setTimeout() hands the browser your callback and returns at once, and only the callback waits.
  • JavaScript on a page does one thing at a time. A loop that waits by checking the clock freezes the page, so clicks and typing do nothing until it ends. A timer that paused your code would freeze Sam’s page for all six minutes.
  • Now and later: the lines around setTimeout() run now, and the callback runs later, when the time’s up and JavaScript has nothing else running.
  • Timers aren’t the only slow things. Loading a file the app ships with, or reading a file the user picks, works the same way. Your code starts it now, and the result arrives later without freezing the page.

The Code

Open your copy of Sam’s recipe box from the last course, or the one in the course’s Sample Code download, folder 01-why-async/recipes-start. Type this into its Console as one entry, pressing Shift+Enter between the lines:

Language: JavaScript
console.log("Eggs on");
setTimeout(() => console.log("Eggs are ready"), 2000);
console.log("Back to the recipe box");

Eggs on and Back to the recipe box appear together, straight away, followed by a muted undefined. That’s the Console’s echo of what the last console.log() returned. Your code didn’t log it. Eggs are ready appears about two seconds later:

Language: Program Output
Eggs on
Back to the recipe box
Eggs are ready
Now Later
console.log("Eggs on"); console.log("Eggs are ready"), after 2 s
setTimeout(...), which only schedules
console.log("Back to the recipe box");

The hand-written wait from the video is a demo, not code to keep:

Language: JavaScript
const doneAt = Date.now() + 3000;
while (Date.now() < doneAt) {}
console.log("Done waiting");

While it runs, clicking the Name field and typing shows nothing. After three seconds, Done waiting appears, and only then do the letters land in the field.

Next, in Follow the Scope Chain, you’ll see how JavaScript finds the names a function uses. That’s the first step to a timer’s callback knowing which pan it’s for.

00:00 Sam’s recipe box is moving into the kitchen. In this video, you’ll make JavaScript run code later, and see why the page stays usable while it waits. Start with the new starter recipe, Soft-boiled eggs.

00:14 They boil for six minutes. Sam wants the box to say when they’re ready. The page has to keep working while the eggs boil.

00:23 For running code later, the browser has setTimeout(). You give it a function and a delay in milliseconds, and it calls your function after the delay. That function is a callback, like the one you pass to addEventListener(): you hand it over, and the browser calls it later.

00:38 Six minutes is a long demo, so this toy egg timer waits two seconds, which is 2000 milliseconds.

00:47 Open Sam’s recipe box, your copy from the last course, and open its Console. Type three lines as one entry, pressing Shift+Enter between them. The first logs Eggs on.

01:00 The second sets the timer, with an arrow that logs Eggs are ready. The third logs Back to the recipe box. If you’ve used a sleep command in another language, you might read setTimeout() the same way: stop here for two seconds, then carry on.

01:16 Your turn: pause the video and predict the order the three lines appear in.

01:26 Press Enter. Eggs on and Back to the recipe box appear together, straight away. The muted undefined under them isn’t logged. It’s the Console’s echo of what the last console.log() returned.

01:39 About two seconds later, Eggs are ready appears at the bottom. So setTimeout() didn’t stop anything. Reading it as a pause was a detour.

01:51 Why doesn’t JavaScript wait right there? To see what waiting would cost, write a wait by hand. This one is a demo, not code to keep. Date.now() gives the current time as a number of milliseconds.

02:05 That’s all you need about it for now. The new entry stores, in doneAt, the time three seconds from now. A while loop with an empty body keeps checking the clock until that time comes.

02:17 Then the entry logs Done waiting. Press Enter, then click the Name field straight away and type Pea. Nothing appears. The page doesn’t react to the click or the keys.

02:29 After three seconds, Done waiting shows up, and only then do the letters land in the field.

02:37 JavaScript on a page does one thing at a time. While your code runs, the page can’t react to a click or a key, so the loop froze Sam’s page. A timer that paused your code would freeze the page the same way, for all six minutes.

02:54 So setTimeout() does something else. It hands the browser your callback and the delay, and it returns at once. Your code carries on to the next line. The browser keeps the clock.

03:06 When the time’s up, and JavaScript has nothing else running, the browser calls your callback. Busy code, or a tab in the background, can make that later than the delay.

03:17 Sort the egg timer’s lines into now and later. Eggs on runs now. The setTimeout() line runs now too, and all it does is schedule. Back to the recipe box runs now.

03:28 Eggs are ready runs later.

03:31 Timers aren’t the only slow things. Loading a file the app ships with, or reading a file the user picks, takes time too. Your code starts them now, and the result arrives later, without freezing the page.

03:43 You’ll meet them later in the course.

03:47 setTimeout() schedules its callback and returns at once. The code after it runs now. The callback runs later.

03:55 Sam’s box will run several timers at once, one for each pan. Each callback runs long after the code around it has moved on. So how does it still know which pan it’s for?

04:06 That starts with how JavaScript finds a name, in Follow the Scope Chain.

Become a Member to join the conversation.