-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-stopwatch.ts
43 lines (34 loc) · 1.54 KB
/
use-stopwatch.ts
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
import { useState, useEffect } from "react";
import { useTakeExamStore } from "@/provider/take-exam/take-exam-store";
const useStopwatch = () => {
const startTimestamp = useTakeExamStore(
(state) => state.timer.startTimestamp
);
const elapsedTimeOnPause = useTakeExamStore(
(state) => state.timer.elapsedTimeOnPause
);
const isRunning = useTakeExamStore((state) => state.timer.isRunning);
const initialTime = useTakeExamStore((state) => state.timer.initialTime);
const pauseTimer = useTakeExamStore((state) => state.timer.pauseTimer);
const startTimer = useTakeExamStore((state) => state.timer.startTimer);
const [displayTime, setDisplayTime] = useState(0);
useEffect(() => {
let intervalId: ReturnType<typeof setInterval>;
if (isRunning && startTimestamp !== null) {
intervalId = setInterval(() => {
const now = Date.now();
// Calcula el tiempo transcurrido desde el inicio
const timeElapsed = now - startTimestamp;
// Suma el tiempo pausado y el tiempo transcurrido, dividiendo por 1000 para obtener segundos completos
const totalElapsed = Math.floor(
(elapsedTimeOnPause + timeElapsed) / 1000
);
// Actualiza el estado `displayTime` sumando el tiempo transcurrido al tiempo inicial
setDisplayTime(initialTime + totalElapsed);
}, 100);
}
return () => clearInterval(intervalId);
}, [isRunning, startTimestamp, elapsedTimeOnPause, initialTime]);
return { displayTime, isRunning, startTimer, pauseTimer };
};
export { useStopwatch };