42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
|
import { useEffect } from "react";
|
||
|
import dayjs from "dayjs";
|
||
|
|
||
|
export default function useRefresh(open, type = "0-clock") {
|
||
|
useEffect(() => {
|
||
|
let timer = null;
|
||
|
if (open) {
|
||
|
switch (type) {
|
||
|
case "0-clock": {
|
||
|
// 0 点
|
||
|
const now = dayjs();
|
||
|
const tomorrow_morning = now.add(1, "day").startOf("day");
|
||
|
timer = setTimeout(() => {
|
||
|
localStorage.setItem('last_refresh', dayjs().format())
|
||
|
document.location.reload();
|
||
|
}, tomorrow_morning.valueOf() - now.valueOf());
|
||
|
break;
|
||
|
}
|
||
|
case "24-hour": {
|
||
|
// 24小时刷新
|
||
|
timer = setTimeout(() => {
|
||
|
const now = dayjs();
|
||
|
localStorage.setItem('last_refresh', now.format())
|
||
|
document.location.reload();
|
||
|
}, 24 * 60 * 60 * 1000);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// 开启
|
||
|
} else {
|
||
|
if (timer) clearTimeout(timer);
|
||
|
timer = null;
|
||
|
}
|
||
|
|
||
|
return () => {
|
||
|
clearTimeout(timer);
|
||
|
};
|
||
|
}, [open, type]);
|
||
|
|
||
|
return undefined;
|
||
|
}
|