mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-20 10:33: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 }) {
|
||||
const [time, setTime] = useState(currentTimeAdjusted(tz))
|
||||
useEffect(() => {
|
||||
const interval = setInterval(() => {
|
||||
setTime(currentTimeAdjusted(tz))
|
||||
}, (1000 - Date.now() % 1000))
|
||||
return () => clearInterval(interval)
|
||||
let interval: number | undefined
|
||||
const updateTime = () => setTime(currentTimeAdjusted(tz))
|
||||
const timeout = setTimeout(() => {
|
||||
interval = setInterval(updateTime, 1000)
|
||||
updateTime()
|
||||
}, (1001 - Date.now() % 1000))
|
||||
return () => interval ? clearInterval(interval) : clearTimeout(timeout)
|
||||
}, [tz])
|
||||
return <div>{time}</div>
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue