Skip to content

Instantly share code, notes, and snippets.

@Kyu
Created November 29, 2024 02:15
Show Gist options
  • Save Kyu/cfb8d39a755e2771ef223fe94608c28f to your computer and use it in GitHub Desktop.
Save Kyu/cfb8d39a755e2771ef223fe94608c28f to your computer and use it in GitHub Desktop.
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