Created
November 29, 2024 02:15
-
-
Save Kyu/cfb8d39a755e2771ef223fe94608c28f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useTimer = () => { | |
const [minutes, setMinutes] = useState(0) | |
const [seconds, setSeconds] = useState(0) | |
const [miliSeconds, setMiliSeconds] = useState(0) | |
const [stop, isSetStop] = useState(false) | |
useEffect(() => { | |
if (stop) return | |
setTimeout(() => { | |
setMiliSeconds((ms) => (ms + 1) % 1000) | |
if (miliSeconds == 0) { | |
setSeconds((s) => (s + 1) % 60) | |
} | |
if (seconds == 0) { | |
setMiliSeconds((m) => m + 1) | |
} | |
}, 10) // used to be 1 | |
}, [miliSeconds, seconds, minutes]) | |
return { | |
values: { | |
miliSeconds, | |
seconds, | |
minutes | |
}, | |
actions: { | |
isSetStop | |
} | |
} | |
} | |
let {xyz} = useTimer() | |
xyz.values | |
xyz.actions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment