Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.
Pallavi Gupta
Last Updated Feb 21, 2025
Advertisement
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.
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.
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. 🚀
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;
// 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;
}
Advertisement
Advertisement
Anuj Sharma
Last Updated Jan 5, 2025
A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.
Anuj Sharma
Last Updated Jan 9, 2025
Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.
Anuj Sharma
Last Updated Feb 13, 2025
Details about how the browser works behind the scenes and what happens when you type google.com in the browser, starting from communication to the webpage rendering.
Anuj Sharma
Last Updated Jan 28, 2025
An Interview-focused explanation of Promise Polyfill in JavaScript which helps to understand both Functional and ES6 custom promise implementation.
Anuj Sharma
Last Updated Jan 29, 2025
Understand the difference between HTTP/2 vs HTTP/1.1 based on the various parameters, which helps to understand the improvement areas of HTTP/2 over HTTP 1.1
Anuj Sharma
Last Updated Dec 10, 2024
A brief explanation of Cross-Origin Resource Sharing (CORS) concept to enable client application accessing resources from cross domain and HTTP headers involved to enable resource access.
© 2024 FrontendGeek. All rights reserved