-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.tsx
58 lines (50 loc) · 1.53 KB
/
Timer.tsx
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
import {useEffect, useState} from 'react';
import Button from './toolbar/Button';
export default function Timer() {
const [startTime, setStartTime] = useState<number>(Date.now());
const [elapsedTime, setElapsedTime] = useState<{
seconds: number;
minutes: number;
hours: number;
}>({seconds: 0, minutes: 0, hours: 0});
function reset() {
setStartTime(Date.now());
setElapsedTime({seconds: 0, minutes: 0, hours: 0});
}
useEffect(() => {
function updateElapsedTime() {
const elapsed = Date.now() - startTime;
const seconds = Math.floor((elapsed / 1000) % 60);
const minutes = Math.floor((elapsed / (1000 * 60)) % 60);
const hours = Math.floor((elapsed / (1000 * 60 * 60)) % 24);
setElapsedTime({seconds, minutes, hours});
}
const intervalHandle = setInterval(() => {
updateElapsedTime();
}, 1000);
return () => {
clearInterval(intervalHandle);
};
}, [startTime]);
return (
<div className="font-mono flex flex-row items-center gap-4">
<div>
{String(elapsedTime.hours).padStart(2, '0')}:
{String(elapsedTime.minutes).padStart(2, '0')}:
{String(elapsedTime.seconds).padStart(2, '0')}
</div>
<div className="">
<Button
border
title="Reset timer"
icon="i-tabler-rotate"
label="reset"
className="important-flex-row important-p1 important-shadow-md gap-1"
onClick={() => {
reset();
}}
/>
</div>
</div>
);
}