🚀 AI SaaS Starter is now live!

50% OFF

Use code FIRST50

Blog/NotesConcept

Implementing a stopwatch using React - Frontend Machine Coding Question

Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.

beginner

Pallavi Gupta

Last Updated Feb 21, 2025


Stopwatch implementation is one of the most commonly asked Frontend Machine Coding Question, and this machine coding question helps to evaluate the understanding of useEffect hook as part of react application. In this post, we will going to understand the implementation of a stopwatch using React that tracks elapsed time in hours, minutes, and seconds.

StopWatch implementation using react

Component States & Hooks

It uses the useState hook to manage 2 component states to track time and status. [ millisecond, setMillisecond] state is used to track the time in milliseconds and [isActive, setIsActive] is used to track the active status of the watch.

The useEffect hook handles the timer functionality, starting an interval that increments the time every second when isActive is true and clearing the interval when paused.

Formatting Functions

A formatting function formatTime converts the elapsed time into a readable HH:MM:SS format. The component displays the time in a styled black-and-white layout and provides three buttons—Start, Pause, and Reset—to control the stopwatch.

Overall, this component demonstrates efficient state management, side effects handling with useEffect, and UI updates in real time, making it a great frontend machine coding question of how to build an interactive timer in React. 🚀

StopWatch React Component

React Component

import { useEffect, useState } from "react";
import "./styles.css";

function StopWatch() {
  const [millisecond, setMillisecond] = useState(0);
  const [isActive, setIsActive] = useState(false);

  useEffect(() => {
    let interval = null;
    if (isActive) {
      interval = setInterval(() => {
        setMillisecond((prevTime) => prevTime + 1);
      }, 1000);
    }
    return () => clearInterval(interval);
  }, [isActive]);

  const formatTime = (seconds) => {
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor(seconds / 60) % 60;
    const sec = Math.floor(seconds % 60);
    return {
      hours: String(hours).padStart(2, "0"),
      minutes: String(minutes).padStart(2, "0"),
      sec: String(sec).padStart(2, "0"),
    };
  };

  const { hours, minutes, sec } = formatTime(millisecond);
  const handleStart = () => {
    setIsActive(true);
  };
  const handlePause = () => {
    setIsActive(false);
  };
  const handleReset = () => {
    setIsActive(false);
    setMillisecond(0);
  };

  return (
    <div className="container">
      <div>
        <h1>Stop Watch</h1>
        <div className="d-flex timeGroup">
          <div className="time">{hours}</div>
          <div className="time">{minutes}</div>
          <div className="time">{sec}</div>
        </div>
        <div className="d-flex buttonGroup">
          <div className="div">
            <button onClick={handleStart}>Start</button>
          </div>
          <div className="div">
            <button onClick={handlePause}>Pause</button>
          </div>
          <div className="div">
            <button onClick={handleReset}>Reset</button>
          </div>
        </div>
      </div>
    </div>
  );
}

export default StopWatch;

Component Styles

// styles.css

.container {
  display: flex;
  justify-content: center;
}

.time {
  width: 33%;
  padding: 0.5rem;
  color: white;
  background-color: skyblue;
  border: 1px solid #eee;
}

.buttonGroup {
  display: flex;
  justify-content: space-between;
  width: 200px;
  margin-top: 1rem;
}

.timeGroup {
  display: flex;
  width: 200px;
}

🚀

Love this content? Share it!

Help others discover this resource

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

Share Your Expertise & Help the Community!

Build Your Portfolio

Help the Community

Strengthen Your Skills

Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀


Other Related Blogs

Top 10 React Performance Optimization Techniques [React Interview]

Anuj Sharma

Last Updated Nov 10, 2025

Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.

Flatten Nested Array in JavaScript using Recursion

Anuj Sharma

Last Updated Nov 24, 2025

Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Implement useThrottle Custom Hook In React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Implement useThrottle Custom Hook In React (Interview) to limit the number of APi calls to improve the performance of application.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2025 FrontendGeek. All rights reserved