const [tabVisibleTime, setTabVisibleTime] = useState<number>(0);
const changeTabRefresh = () => {
// 用户息屏、或者切到后台运行 (离开页面)
if (document.visibilityState === 'hidden') {
// console.log('hidden')
}
// 用户打开或回到页面
if (document.visibilityState === 'visible') {
setTabVisibleTime(new Date().getTime());
}
}
useEffect(() => {
document.addEventListener('visibilitychange', changeTabRefresh);
return () => {
// 销毁tab切换的逻辑
document.removeEventListener('visibilitychange', changeTabRefresh);
};
}, [])
useEffect(() => {
//监听到界面唤醒后需要做的操作
const checkMapVersion = async (id: number, mapId: number) => {
const res = await mapIsLatest(initData.warehouseId || 1, {
id,
mapId,
});
if(res?.success && !res?.data) {
window.location.reload();
}
};
if(mapInfo) {
checkMapVersion(mapInfo.curMapId, mapInfo.curId);
}
}, [tabVisibleTime])
监听当前界面 用户息屏、或者切到后台运行 (离开页面)需要做的操作
于 2024-07-25 09:35:01 首次发布