JavaScript setTimeout — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript setTimeout lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Run something later
easy10 minUse setTimeout to report done after a short delay.
- 2
Cancel a timer
easy10 minSchedule a timer, cancel it, and report cancelled instead.
- 3
Pass an argument
easy10 minPass a name through setTimeout and greet it.
- 4
Chain two timers
easy10 minSchedule a second timer from inside the first and report both steps.
- 5
Check the delay was respected
medium10 minReport that at least ten milliseconds passed.
Find the bug(5)
- 1
Fix the handler called immediately
easy8 minBrackets run the function now instead of scheduling it.
- 2
Fix the timer never cancelled
easy8 minThe id is not kept, so clearTimeout has nothing to cancel.
- 3
Fix clearInterval used on a timeout
easy8 minA timeout is cancelled with clearTimeout.
- 4
Fix the argument never passed
easy8 minExtra arguments go after the delay, not before it.
- 5
Fix the second timer scheduled too early
easy8 minBoth timers were started at once, so the order is not guaranteed.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace