mirror of
https://github.com/DustinBrett/daedalOS.git
synced 2025-12-06 12:20:20 +01:00
23 lines
570 B
TypeScript
23 lines
570 B
TypeScript
import { useEffect, useState } from "react";
|
|
import { MILLISECONDS_IN_SECOND } from "utils/constants";
|
|
|
|
const useClock = (): Date => {
|
|
const [now, setNow] = useState(new Date());
|
|
const updateClock = () => setNow(new Date());
|
|
|
|
useEffect(() => {
|
|
let timeoutId: NodeJS.Timeout;
|
|
|
|
timeoutId = setTimeout(() => {
|
|
updateClock();
|
|
timeoutId = setInterval(updateClock, MILLISECONDS_IN_SECOND);
|
|
}, MILLISECONDS_IN_SECOND - new Date().getMilliseconds());
|
|
|
|
return () => clearTimeout(timeoutId);
|
|
}, []);
|
|
|
|
return now;
|
|
};
|
|
|
|
export default useClock;
|