时间自动更新

import { useCallback, useEffect, useState } from 'react';
import dayjs from 'dayjs';

const useShowTime = () => {
  const getCurrTime = () => dayjs().format('YYYY-MM-DD HH:mm:ss');

  const [time, setTime] = useState(getCurrTime());

  const showTime = useCallback(() => {
    let timer: NodeJS.Timeout;
    const fn = () => {
      setTime(getCurrTime());
      if (timer) clearTimeout(timer);
      setTimeout(fn, 1000);
    };
    fn();
  }, []);

  useEffect(() => {
    showTime();
  }, [showTime]);

  return time;
};

export { useShowTime };
最近修改时间:2024-08-14 06:54:07