typescript · level 2 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- 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.
guess first
A timer of zero. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
setTimeout(() => console.log("A"), 0);
console.log("B");
setTimeout(() => console.log("C"), 50);
console.log("D");
your turn
Your turn. Count down to lift-off.
Fill in the two missing delays, in milliseconds, so the countdown shows 3, 2, 1 and then Lift-off!, in that order.
fix it
The alarm beeps once too often. Fix it!
- No error, but there are four beeps. When does the interval get cleared?
- After the third beep,
beepsis 3, but3 > 3isfalse, so it keeps going. - Change
beeps > 3tobeeps === 3on line 5, then press run.
nailed it
your code can wait for the right moment. :D
setTimeoutruns a callback after a number of milliseconds.- Asynchronous code does not finish in the order it is written.
setIntervalrepeats untilclearIntervalstops it.
new words in your collection
- setTimeout
- asynchronous
This lesson isn’t saved. A free account remembers every lesson you finish.