[英]Logout user when browser close
I would like log out user when he/she close the browser, but not tab.我想在用户关闭浏览器而不是标签时注销用户。
I'm aware with the 'beforeunload' event-listener from JS, but that also being call when close tab.我知道来自 JS 的 'beforeunload' 事件侦听器,但在关闭选项卡时也会调用它。
I have a JS function called logout().我有一个名为 logout() 的 JS function。 How could I call this when user close browser?
当用户关闭浏览器时,我怎么能调用它?
You could compare the timestamp of the close event.您可以比较关闭事件的时间戳。 The browser need more time to close all tabs and so there are a little delay.
浏览器需要更多时间来关闭所有选项卡,因此会有一点延迟。
//Unload Events
$(window).on('unload',function(e) {
e = e || window.event;
var timestamp = localStorage.getItem('timestamp')
if(timestamp !=null && timestamp !=undefined){
var TimeNow = new Date().getTime(), Difference = TimeNow - timestamp ;
if(Difference<20){
//Browser Closed
console.log('Browser Closed')
localStorage.removeItem('timestamp');
//Your logout function
logout();
}else{
//Tab Closed
console.log('Browser Tab Closed');
localStorage.setItem('timestamp',new Date().getTime());
}
}else{
//Other Event
localStorage.setItem('timestamp',new Date().getTime());
}
});
There no other solution for this detection.此检测没有其他解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.