Open In App

Lap Memory Stopwatch using React

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
1 Like
Like
Report

Stopwatch is an application which helps to track time in hours, minutes, seconds, and milliseconds. This application implements all the basic operations of a stopwatch such as start, pause and reset button. It has an additional feature using which we can keep a record of laps which is useful when we have to note time for certain checkpoints.

Final Preview

gfg

Prerequisites

Approach

This program creates a functional stopwatch timer with start, pause, reset, and lap features. It maintains state variables for hours, minutes, seconds, and milliseconds, ensuring real-time updates using the "useEffect" hook. Proper interval clearing prevents memory leaks, delivering a straightforward and reliable stopwatch for time tracking.

Functionalities:

  • When users click "Start," the timer begins, disabling the button with a "not-allowed" cursor.
  • "Pause" stops the timer, and "Reset" sets all time units to zero.
  • The program also offers a "Lap" feature for recording lap times, allowing users to track multiple intervals

Steps to create the application

Step 1: Set up React project using the command

npm create vite@latest <<name-of-project>> --template react

Step 2: Navigate to the project folder using

cd <<name-of-project>>

Step 3: Install the required dependencies:

npm install

Step 4: Create a folder “components” and add three new files in it namely StopWatch.js, Lap.js and StopWatch.css.

Project Structure:

Screenshot-2023-09-19-101917
Project Structure

The updated dependencies in package.json will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"vite": "^4.0.0"
}

Example: Write the following code in respective files

  • App.js: This file imports the StopWatch components and exports it.
  • StopWatch.js: This file contains the logic to track time in hours, minutes, seconds, and milliseconds, offering user-friendly controls for starting, pausing, and resetting the timer.
  • Lap.js : This file contains the logic for displaying the laps in a list. From StopWatch.js file we send a prop laps to this file.
  • StopWatch.css: This file contains the design of the StopWatch.js elements.
CSS
/* StopWatch.css */

body {
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

.container {
    padding: 30px;
    border: 1px solid #ced4da;
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.25);
    margin: 5px;
    width: 100%;
    max-width: 400px;
    box-sizing: border-box;
}

.timeDisplay {
    padding: 20px;
    font-size: 30px;
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.25);
    border-radius: 0.25em;
    text-align: center;
}

.buttons {
    display: flex;
    justify-content: center;
    margin-top: 20px;
}

button.btn {
    padding: 10px 20px;
    outline: none;
    border: 1px solid #ced4da;
    margin: 5px;
    border-radius: 4px;
    cursor: pointer;
    background: #1d9bf0;
    color: #fff;
    font-size: 16px;
    transition: all 0.5s;
}

button.btn:hover {
    background-color: #1879ba;
}

.laps {
    margin-top: 20px;
}

.laps h3 {
    text-align: left;
    display: flex;
    justify-content: space-between;
    margin: 5px;
}

.laps ul {
    list-style-type: none;
    padding: 0;
}

.laps li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0px 5px 10px 5px;
}

@media screen and (max-width: 420px) {
    .buttons {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }
}
JavaScript JavaScript JavaScript

To start the application run the following command

npm start

Output


Next Article

Similar Reads