From 2bc608e332c1b20ff73fa8d4e47e9d2fbc8577d1 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Tue, 31 Dec 2024 23:45:26 +0800 Subject: [PATCH] feat: refactor now time --- app/(main)/header.tsx | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/app/(main)/header.tsx b/app/(main)/header.tsx index 25c6148..c435759 100644 --- a/app/(main)/header.tsx +++ b/app/(main)/header.tsx @@ -97,34 +97,25 @@ function Links() { ) } -// https://github.com/streamich/react-use/blob/master/src/useInterval.ts -const useInterval = (callback: () => void, delay: number | null) => { - const savedCallback = useRef<() => void>(() => {}) - useEffect(() => { - savedCallback.current = callback - }) - useEffect(() => { - if (delay !== null) { - const interval = setInterval(() => savedCallback.current(), delay || 0) - return () => clearInterval(interval) - } - return undefined - }, [delay]) -} function Overview() { const t = useTranslations("Overview") const [mouted, setMounted] = useState(false) useEffect(() => { setMounted(true) }, []) - const timeOption = DateTime.TIME_SIMPLE + const timeOption = DateTime.TIME_WITH_SECONDS timeOption.hour12 = true const [timeString, setTimeString] = useState( DateTime.now().setLocale("en-US").toLocaleString(timeOption), ) - useInterval(() => { - setTimeString(DateTime.now().setLocale("en-US").toLocaleString(timeOption)) - }, 1000) + useEffect(() => { + const updateTime = () => { + const now = DateTime.now().setLocale("en-US").toLocaleString(timeOption) + setTimeString(now) + requestAnimationFrame(updateTime) + } + requestAnimationFrame(updateTime) + }, []) return (

{t("p_2277-2331_Overview")}