监听浏览器是关闭还是刷新

2024-03-24  本文已影响0人  码农界四爷__King
function close() {
    console.log('===============close=========')
    let _beforeUnload_time = 0,
        _unload_time = 0
    window.addEventListener('beforeunload', () => {
        console.log('beforeunload----------刷新')
        _beforeUnload_time = new Date().getTime()
    })
    window.addEventListener('unload', () => {
        _unload_time = new Date().getTime()
        if (_unload_time - _beforeUnload_time <= 1) {
            window.localStorage.clear()
            console.log('unload---------关闭')
        }
    })
}

上一篇下一篇

猜你喜欢

热点阅读