×

typescript · level 2 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

runs a callback later, and the program carries on.

Sometimes code should wait: a reminder in ten minutes, or a light that turns off after a second. setTimeout takes a callback and a number of milliseconds, each a thousandth of a second. It books the callback, then your program carries straight on.

That makes the code : things do not finish in the order they are written. Lines after setTimeout run first, and the callback runs when its time is up.

setInterval repeats a callback every so many milliseconds, until you stop it with clearInterval.

console.log("Put the kettle on");
setTimeout(() => {
  console.log("Kettle boiled!");
}, 300);
setTimeout(() => console.log("Toast popped!"), 100);
console.log("Read the news");

Gwecode waits up to five seconds for your timers before the run ends.