-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopWatch
68 lines (57 loc) · 1.42 KB
/
StopWatch
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
import React, { useState } from "react";
const title = "Let's start the timer";
function padTime(value) {
return value.toString().padStart(2, "0");
}
export default function App() {
const [timeLeft, setTimeLeft] = useState(25 * 60)
//replace the 25 for minutes
const min = padTime(Math.floor(timeLeft / 60));
const sec = padTime(timeLeft - min * 60);
const addStart = () => {
// const enterMin = (prompt("Enter minutes"));
// const enterSec = prompt("Enter seconds");
// const totalTime = enterMin * 60 + enterSec;
let times;
// if (totalTime > 0) {
// setTimeLeft(totalTime);
times = setInterval(() => {
setTimeLeft((tl) => {
if (tl > 1) return tl - 1;
else {
clearInterval(times); // Stop the timer when it reaches 0
return 0;
}
});
}, 1000);
// }
};
const addReset = (e) => {
setTimeLeft((prev) => {
if (prev) return 0;
});
};
const addStop = (e) => {
setTimeLeft((tl) => {
if (tl) return 0;
clearInterval(tl);
});
};
return (
<div className="App">
{title}
<div>
{min}:{sec}
</div>
<div>
<button onClick={addStart}>Start</button>
<button disabled={timeLeft === 0} onClick={addStop}>
Stop
</button>
<button disabled={timeLeft === 0} onClick={addReset}>
Reset
</button>
</div>
</div>
);
}