0
window.onbeforeunload = closingCode;
    function closingCode(e) {
        var evtobj = window.event ? event : e;

        if (evtobj == e) {
            //firefox
            if (!evtobj.clientY) {
if(isRefreshingOrClosingEvent() === 'will be refreshed'){
                      nothing;
                    }
                  if(isRefreshingOrClosingEvent() === 'will be closed'){
                      refreshToken()
                   }
            }
        }
        else {
            //IE
            if (evtobj.clientY < 0) {
                 if(isRefreshingOrClosingEvent() === 'will be refreshed'){
                      nothing;
                    }
                  if(isRefreshingOrClosingEvent() === 'will be closed'){
                      refreshToken()
                   }
            }
        }

        return null;
    }

function isRefreshingOrClosingEvent(){
     if(pageWillBeRefreshed){
            return 'will be refreshed'
     }
     if(pageWillBeCloseded){
            return 'will be closed'
     }
}

function refreshToken(){
    API.refreshToken();
}

How to detect closing event or refreshing event before will happened?

I want that because for example I have 60 minutes lifetime for a token. After 20 minutes I will refresh the page => 40 minutes lifetime, But if I close the page I whould have still 40 minutes. Only if the closing event will be triggered I want to refresh that token to have 60 minutes. May be after 50 minutes I will open the tab and I want to have 10 more minutes!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alex
  • 1,013
  • 1
  • 13
  • 27
  • *"How to detect closing event or refreshing event before will happened?"* That's what the event you're using above (`onbeforeunload`) does, notifies your code before page refresh or close. There are **strict** limits on what you can do within a handler for that event, but that's the event. – T.J. Crowder Mar 05 '18 at 08:29
  • This can be unnecessary evil, since this will require to do *synchronous* XHR call, which can spoil user experience. Still, the event can be ignored by browsers. – Estus Flask Mar 05 '18 at 08:32

0 Answers0