Skip to content

Commit f31a157

Browse files
project(stopwatch): Application completed (#18)
2 parents e942806 + 21c6264 commit f31a157

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/App.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import HandlingForm from "./components/Hooks/useRefHook/HandlingForm";
88
import FormDetails from "./tryityourself/Akash/FormDetails";
99
import RandomBackground from "./tryityourself/Akash/RandomBackground";
1010

11+
// PROJECTS
12+
import StopWatch from "./project/StopWatch/StopWatch";
13+
1114
const App = () => {
12-
return <RandomBackground />;
15+
return <StopWatch />;
1316
};
1417

1518
export default App;

src/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
@import "./components/Hooks/useRefHook/HandlingForm.style.css";
33
@import "./tryityourself/Akash/FormDetails.style.css";
44
@import "./tryityourself/Akash/RandomBackground.style.css";
5+
@import "./project/StopWatch/StopWatch.style.css";

src/project/StopWatch/StopWatch.jsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { useEffect, useRef, useState } from "react";
2+
3+
// ICONS
4+
import { FaPlay } from "react-icons/fa";
5+
import { FaPause } from "react-icons/fa6";
6+
import { LuTimerReset } from "react-icons/lu";
7+
8+
const StopWatch = () => {
9+
// STATES
10+
const [isRunning, setIsRuning] = useState(() => {
11+
return false;
12+
});
13+
const [elapsedTime, setElapsedTime] = useState(0);
14+
const startTimeRef = useRef(0);
15+
const intervalTimeRef = useRef(null);
16+
17+
// SIDE-EFFECTS
18+
useEffect(() => {
19+
if (isRunning) {
20+
startTimeRef.current = Date.now() - elapsedTime;
21+
intervalTimeRef.current = setInterval(() => {
22+
setElapsedTime(Date.now() - startTimeRef.current);
23+
}, 10);
24+
25+
return () => clearInterval(intervalTimeRef.current);
26+
}
27+
}, [isRunning]);
28+
29+
// HANDLER FUNCTIONS
30+
const handleStartOrStop = () => {
31+
return setIsRuning((previous) => !previous);
32+
};
33+
34+
const handleReset = () => {
35+
setIsRuning(false);
36+
setElapsedTime(0);
37+
};
38+
39+
const displayStopWatchTime = (elapsedTime) => {
40+
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
41+
let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
42+
let seconds = Math.floor((elapsedTime / 1000) % 60);
43+
let milliseconds = Math.floor((elapsedTime % 1000) / 10);
44+
return `
45+
${hours.toString().padStart(2, "0")}:
46+
${minutes.toString().padStart(2, "0")}:
47+
${seconds.toString().padStart(2, "0")}:
48+
${milliseconds.toString().padStart(2, "0")}`;
49+
};
50+
51+
return (
52+
<div className="CONTAINER">
53+
<div className="text-6xl">{displayStopWatchTime(elapsedTime)}</div>
54+
<div className="mt-3 flex items-center justify-center gap-3">
55+
<button className="BUTTON" onClick={handleStartOrStop}>
56+
{isRunning ? <FaPause size={20} /> : <FaPlay size={20} />}
57+
</button>
58+
<button className="BUTTON" onClick={handleReset}>
59+
<LuTimerReset size={20} />
60+
</button>
61+
</div>
62+
</div>
63+
);
64+
};
65+
66+
export default StopWatch;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.CONTAINER {
2+
@apply h-screen w-full flex flex-col items-center justify-center bg-slate-500 text-black;
3+
}
4+
5+
.BUTTON {
6+
@apply p-2 rounded-md border-1 border-black hover:bg-black hover:text-white hover:cursor-pointer;
7+
}

0 commit comments

Comments
 (0)