-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56. Countdown timer(with styles).tsx
More file actions
82 lines (70 loc) · 1.83 KB
/
Copy path56. Countdown timer(with styles).tsx
File metadata and controls
82 lines (70 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useState, useEffect } from "react";
const App: React.FC = () => {
// initial countdown value
const initialTime = 10;
// state to track remaining time and timer status
const [timeLeft, setTimeLeft] = useState<number>(initialTime);
const [isRunning, setIsRunning] = useState<Boolean>(false);
// useEffect to handle the countdown logic
useEffect(() => {
let timer: NodeJS.Timeout;
if (isRunning && timeLeft > 0) {
timer = setInterval(() => {
setTimeLeft((prev) => prev - 1);
}, 1000);
}
if (timeLeft === 0) {
setIsRunning(false);
}
return () => clearInterval(timer);
}, [isRunning, timeLeft]);
// start and reset handlers
const startCountdown = () => {
if (timeLeft > 0) {
setIsRunning(true);
}
};
const resetCountdown = () => {
setIsRunning(false);
setTimeLeft(initialTime);
};
// styling for the dark mode
const containerStyle: React.CSSProperties = {
height: "100vh",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#121212",
color: "#ffffff",
fontFamily: "monospace",
};
const timeStyle: React.CSSProperties = {
fontSize: "4rem",
marginBottom: "20px",
};
const buttonStyle: React.CSSProperties = {
margin: "5px",
padding: "10px 20px",
fontSize: "1rem",
cursor: "pointer",
border: "none",
borderRadius: "5px",
backgroundColor: "#1f1f1f",
color: "#ffffff",
};
return (
<div style={containerStyle}>
<div style={timeStyle}>{timeLeft}</div>
<div>
<button onClick={startCountdown} style={buttonStyle}>
Start
</button>
<button onClick={resetCountdown} style={buttonStyle}>
Reset
</button>
</div>
</div>
);
};
export default App;