41 - Stopwatch app that tracks elapsed time

Description

Create a stopwatch app that tracks elapsed time, visually displays the elapsed time in a clear format (minutes, seconds, milliseconds), offers start, stop, and reset buttons, continues to count time even when the browser tab is inactive, measures lap times, and sounds alerts.

Algorithm

  1. Create a React component for the stopwatch app

  2. Define the state variables for the elapsed time, lap times, and timer status

  3. Use the useState hook to manage the state variables

  4. Create functions for starting, stopping, and resetting the timer

  5. Create a function for measuring lap times

  6. Use the useEffect hook to update the elapsed time and lap times

  7. Render the stopwatch display, buttons, and lap times list

Classes

  • Stopwatch: The main stopwatch component

  • StopwatchDisplay: The component that displays the elapsed time

  • StopwatchButtons: The component that renders the start, stop, and reset buttons

  • LapTimesList: The component that displays the lap times list

Code

Stopwatch.js

StopwatchDisplay.js

StopwatchButtons.js

LapTimesList.js

Explanation

The code creates a stopwatch app that tracks elapsed time, visually displays the elapsed time in a clear format (minutes, seconds, milliseconds), offers start, stop, and reset buttons, continues to count time even when the browser tab is inactive, measures lap times, and sounds alerts. The component uses the useState hook to manage the state variables and the useEffect hook to update the elapsed time and lap times.

Possible Future Enhancements

  • Add support for custom timer intervals

  • Add support for timer notifications

  • Add support for timer statistics (e.g. average lap time)

Last updated