Skip to content

project(stopwatch): Application completed #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import HandlingForm from "./components/Hooks/useRefHook/HandlingForm";
import FormDetails from "./tryityourself/Akash/FormDetails";
import RandomBackground from "./tryityourself/Akash/RandomBackground";

// PROJECTS
import StopWatch from "./project/StopWatch/StopWatch";

const App = () => {
return <RandomBackground />;
return <StopWatch />;
};

export default App;
1 change: 1 addition & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
@import "./components/Hooks/useRefHook/HandlingForm.style.css";
@import "./tryityourself/Akash/FormDetails.style.css";
@import "./tryityourself/Akash/RandomBackground.style.css";
@import "./project/StopWatch/StopWatch.style.css";
66 changes: 66 additions & 0 deletions src/project/StopWatch/StopWatch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useEffect, useRef, useState } from "react";

// ICONS
import { FaPlay } from "react-icons/fa";
import { FaPause } from "react-icons/fa6";
import { LuTimerReset } from "react-icons/lu";

const StopWatch = () => {
// STATES
const [isRunning, setIsRuning] = useState(() => {
return false;
});
const [elapsedTime, setElapsedTime] = useState(0);
const startTimeRef = useRef(0);
const intervalTimeRef = useRef(null);

// SIDE-EFFECTS
useEffect(() => {
if (isRunning) {
startTimeRef.current = Date.now() - elapsedTime;
intervalTimeRef.current = setInterval(() => {
setElapsedTime(Date.now() - startTimeRef.current);
}, 10);

return () => clearInterval(intervalTimeRef.current);
}
}, [isRunning]);

// HANDLER FUNCTIONS
const handleStartOrStop = () => {
return setIsRuning((previous) => !previous);
};

const handleReset = () => {
setIsRuning(false);
setElapsedTime(0);
};

const displayStopWatchTime = (elapsedTime) => {
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
let seconds = Math.floor((elapsedTime / 1000) % 60);
let milliseconds = Math.floor((elapsedTime % 1000) / 10);
return `
${hours.toString().padStart(2, "0")}:
${minutes.toString().padStart(2, "0")}:
${seconds.toString().padStart(2, "0")}:
${milliseconds.toString().padStart(2, "0")}`;
};

return (
<div className="CONTAINER">
<div className="text-6xl">{displayStopWatchTime(elapsedTime)}</div>
<div className="mt-3 flex items-center justify-center gap-3">
<button className="BUTTON" onClick={handleStartOrStop}>
{isRunning ? <FaPause size={20} /> : <FaPlay size={20} />}
</button>
<button className="BUTTON" onClick={handleReset}>
<LuTimerReset size={20} />
</button>
</div>
</div>
);
};

export default StopWatch;
7 changes: 7 additions & 0 deletions src/project/StopWatch/StopWatch.style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.CONTAINER {
@apply h-screen w-full flex flex-col items-center justify-center bg-slate-500 text-black;
}

.BUTTON {
@apply p-2 rounded-md border-1 border-black hover:bg-black hover:text-white hover:cursor-pointer;
}