使用window的原生方法beforeunload
window.addEventListener('beforeunload', e => {
navigator.sendBeacon(this.root+this.root2 +23333+'/'+9090909+ '/' + getToken() )
})
关于navigator.sendBeacon
在 web 开发中,我们经常需要将用户行为或性能数据上报到服务器。为了不影响用户体验,开发者通常会在页面卸载时进行数据上报。然而,传统的数据上报方式,如 XMLHttpRequest 或 Fetch API,容易受到页面卸载过程中的阻塞,导致数据丢失。为了解决这个问题,navigator.sendBeacon API 被引入,它可以在页面卸载时安全、可靠地发送数据。
navigator.sendBeacon
关于beforeunload
onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发。 onbeforeunload