Command Palette

Search for a command to run...

Hectal
Phase 7: Effects
React · Exercise 2 of 5 PreviousNext
Intermediate

A stopwatch that really stops

Build a stopwatch. #time shows the elapsed seconds with one decimal (0.0, 0.1, …). The button #toggle says Start when stopped and Stop while running.

While running, an interval adds one tenth of a second every 100 ms. When stopped, the time must freeze: the interval has to be cleared, not left running in the background.

Use an effect that depends on running, starts an interval when it is true, and returns a cleanup.

What Check tests

  1. It starts at 0.0 with a Start button
  2. After Start, the time goes up
  3. After Stop, the time freezes
  4. Start again continues from where it stopped
  5. The effect clears its interval

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.