mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-19 18:13:41 -05:00
Synchronize clock to start of second
This commit is contained in:
parent
0e4addd3fc
commit
6b7945c50b
1 changed files with 7 additions and 4 deletions
|
@ -29,10 +29,13 @@ const currentTimeAdjusted = (tz: string) => {
|
||||||
function ClockElement({ tz }: { tz: string }) {
|
function ClockElement({ tz }: { tz: string }) {
|
||||||
const [time, setTime] = useState(currentTimeAdjusted(tz))
|
const [time, setTime] = useState(currentTimeAdjusted(tz))
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = setInterval(() => {
|
let interval: number | undefined
|
||||||
setTime(currentTimeAdjusted(tz))
|
const updateTime = () => setTime(currentTimeAdjusted(tz))
|
||||||
}, (1000 - Date.now() % 1000))
|
const timeout = setTimeout(() => {
|
||||||
return () => clearInterval(interval)
|
interval = setInterval(updateTime, 1000)
|
||||||
|
updateTime()
|
||||||
|
}, (1001 - Date.now() % 1000))
|
||||||
|
return () => interval ? clearInterval(interval) : clearTimeout(timeout)
|
||||||
}, [tz])
|
}, [tz])
|
||||||
return <div>{time}</div>
|
return <div>{time}</div>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue