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!