Home > Web Front-end > JS Tutorial > Watch: Adding a Lap Logger to a React Stopwatch

Watch: Adding a Lap Logger to a React Stopwatch

Jennifer Aniston
Release: 2025-02-19 11:24:10
Original
519 people have browsed it

Watch: Adding a Lap Logger to a React Stopwatch

This tutorial extends our React stopwatch to include lap timing functionality – a crucial feature for any serious timekeeping application. We'll leverage our knowledge of React state management, arrays, and conditional rendering to achieve this. This is part 3 of the Building a Stopwatch in React series.

Frequently Asked Questions (FAQs) about Adding Lap Logging to a React Stopwatch

How to Add Lap Logging to a React Stopwatch?

Adding lap logging requires a function triggered by a "Lap" button. This function captures the stopwatch's current time and adds it to an array. The useState hook manages this array.

const [laps, setLaps] = useState([]);

const handleLap = () => {
  setLaps([...laps, currentTime]);
};
Copy after login

Displaying Lap Times

Display lap times by mapping over the laps array, rendering a component for each lap:

{laps.map((lap, index) => (
  <p key={index}>Lap {index + 1}: {lap}</p>
))}
Copy after login

Formatting Lap Times

Improve readability by converting milliseconds to minutes, seconds, and milliseconds using a helper function:

const formatTime = (time) => {
  const minutes = Math.floor(time / 60000);
  const seconds = Math.floor((time - minutes * 60000) / 1000);
  const milliseconds = time - minutes * 60000 - seconds * 1000;
  return `${minutes}:${seconds}:${milliseconds}`;
};
Copy after login

Resetting Lap Times

Resetting lap times involves clearing the laps array:

const handleReset = () => {
  setLaps([]);
  // ...reset stopwatch...
};
Copy after login

Ensuring Accurate Lap Times

Accuracy is paramount. The lap logging function should be synchronous to avoid delays in capturing the current time.

Adding Pause Functionality

Introduce a state variable to track whether the stopwatch is running:

const [isRunning, setIsRunning] = useState(false);

// ...in the time increment function...
if (isRunning) {
  // ...increment time...
}
Copy after login

Resuming from Pause

Preserve the current time when pausing; only reset it during a full reset.

Implementing Lap Functionality (already covered above)

Persisting Lap Times Across Refreshes

Use localStorage to store lap times, ensuring persistence even after page refreshes:

const handleLap = () => {
  const newLaps = [...laps, currentTime];
  setLaps(newLaps);
  localStorage.setItem('laps', JSON.stringify(newLaps));
};
Copy after login

Responsive Design

Use CSS media queries to adjust the layout and sizing for different viewport sizes:

@media (max-width: 600px) {
  .stopwatch {
    font-size: 20px;
  }
  .lap-logger {
    font-size: 16px;
  }
}
Copy after login

The above is the detailed content of Watch: Adding a Lap Logger to a React Stopwatch. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template