import {useEffect, useState} from 'react'; import Button from './toolbar/Button'; export default function Timer() { const [startTime, setStartTime] = useState(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 (
{String(elapsedTime.hours).padStart(2, '0')}: {String(elapsedTime.minutes).padStart(2, '0')}: {String(elapsedTime.seconds).padStart(2, '0')}
); }