页面离开,刷新,加载,分别执行方法

页面加载时只执行onload 
页面关闭时先执行onbeforeunload,最后onunload 
页面刷新时先执行onbeforeunload,然后onunload,最后onload  

 刷新和关闭都会执行

window.onbeforeunload =  async function(){
    // debugger
    await handleDeparture()  
}


async function handleDeparture(){
    await getDeparture({journalId:localStorage.journalId}).then(res=>{
            console.log("getDeparture",res)
    })
}

接口要写成异步的

,如果是ajax,要换一个属性值,可自行百度。

 只关闭的时候执行

async function handleDeparture(){
    await getDeparture({journalId:localStorage.journalId}).then(res=>{
            console.log("getDeparture",res)
    })
}


window.onunload =  async function(){
    // debugger
    await handleDeparture()  
}

 

posted @ 2020-12-29 15:44  sinceForever  阅读(298)  评论(0编辑  收藏  举报