add refresher
This commit is contained in:
41
src/hooks/useRefresh.js
Normal file
41
src/hooks/useRefresh.js
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user