on unload event of browser

前端 未结 2 1319
不知归路
不知归路 2021-01-15 11:31

HI

I wanna timeout session from client page,i tried below code but not able to execute code need some help of how i can handle OK or CANCEL events from user

2条回答
  •  醉梦人生
    2021-01-15 12:08

    onbeforeunload must return a string, the browser creates the confirm box from that string. If you want to capture that the user didn't leave (You cannot run anything if the user left, thats just how it is), simply set a timer and see if it fires:

    window.onbeforeunload = function(){
        setTimeout(function(){
            //User didnt leave
            goDoFunStuff();
        },500);
        return 'wanna go?'
    }
    

    Since onbeforeunload is blocking the timer will only fire if the user didnt leave

提交回复
热议问题