Skip to main content

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. 1

    Run something later

    easy10 min

    Use setTimeout to report done after a short delay.

  2. 2

    Cancel a timer

    easy10 min

    Schedule a timer, cancel it, and report cancelled instead.

  3. 3

    Pass an argument

    easy10 min

    Pass a name through setTimeout and greet it.

  4. 4

    Chain two timers

    easy10 min

    Schedule a second timer from inside the first and report both steps.

  5. 5

    Check the delay was respected

    medium10 min

    Report that at least ten milliseconds passed.

Find the bug(5)

  1. 1

    Fix the handler called immediately

    easy8 min

    Brackets run the function now instead of scheduling it.

  2. 2

    Fix the timer never cancelled

    easy8 min

    The id is not kept, so clearTimeout has nothing to cancel.

  3. 3

    Fix clearInterval used on a timeout

    easy8 min

    A timeout is cancelled with clearTimeout.

  4. 4

    Fix the argument never passed

    easy8 min

    Extra arguments go after the delay, not before it.

  5. 5

    Fix the second timer scheduled too early

    easy8 min

    Both 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