1.監(jiān)聽(tīng) visibility change 事件? ?可以監(jiān)聽(tīng)瀏覽器是否離開(kāi)
2. 計(jì)算離開(kāi)的時(shí)間
document.addEventListener('visibilitychange', ()=>{
// 頁(yè)面變?yōu)椴豢梢?jiàn)時(shí)觸發(fā)
? ? if (document.visibilityState == 'hidden') {
this.leaveTime = parseInt(new Date().getTime()/ 1000);
}
// 頁(yè)面變?yōu)榭梢?jiàn)時(shí)觸發(fā)
? ? if (document.visibilityState == 'visible') {
let nowTime = parseInt(new Date().getTime()/ 1000);
let currentTime = nowTime - this.leaveTime;//離開(kāi)的時(shí)間
}
});