You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

6 months ago
import { onBeforeUnmount } from 'vue';
interface UseTableRefresh {
reload: Function;
props: Recordable;
}
export function useTableRefresh({ props, reload }: UseTableRefresh) {
let refreshInterval: NodeJS.Timer;
/**
*
* @param immediate
*/
function delayFetch(immediate = false) {
const { refreshTime } = props;
if (refreshTime > 0) {
console.log(`页面自动刷新开启,间隔${refreshTime}`);
refreshInterval && clearInterval(refreshInterval);
refreshInterval = setInterval(reload.bind(undefined, false), refreshTime * 1000);
immediate && reload.bind(undefined, false)();
}
}
/**
*
*/
function clearRefreshInterval() {
const { refreshTime } = props;
if (refreshTime > 0) {
console.log(`页面自动刷新停止`);
refreshInterval && clearInterval(refreshInterval);
}
}
/**
*
*/
function visibilitychange() {
if (document.visibilityState === 'visible') {
delayFetch(true);
} else {
console.log(`转入后台`);
clearRefreshInterval();
}
}
document.addEventListener('visibilitychange', visibilitychange);
onBeforeUnmount(() => {
console.log(`页面退出`);
clearRefreshInterval();
document.removeEventListener('visibilitychange', visibilitychange);
});
return {
delayFetch,
};
}