作者:诸暨巴黎婚纱 | 来源:互联网 | 2023-09-08 12:23
现在碰到的问题就是关闭页面时axios请求总提交不了,请问有什么解决的方法么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| window.addEventListener('beforeunload', function() {
beforeUnloadTimestamp = new Date().getTime();
})
window.addEventListener('unload', function() {
beforeUnloadTimestamp = beforeUnloadTimestamp || 0;
localStorage.setItem('onload', new Date().getTime());
// 测试chrome和ie11 得出,chrome,IE11 // 刷新时 chrome >=11ms ,ie11 >=2ms 左右, 在关闭时,chrome间隔在 >=2ms,IE11 >=3000ms
if (isIE) {
if (new Date().getTime() - beforeUnloadTimestamp > 500) {
isClose = true;
}
} else {
if (new Date().getTime() - beforeUnloadTimestamp <5) {
isClose = true;
}
}
if (isClose) {
axios.get('/au/logout').then(res => {
window.sessionStorage.clear();
});
localStorage.setItem('close', new Date().getTime());
}else{
localStorage.setItem('refresh', new Date().getTime());
}
}) |