用户关闭浏览器时如何触发

时间:2020-04-23 12:08:59

标签: angular

如果用户关闭浏览器,则需要触发,以便在15分钟的浏览器会话结束后将用户的状态从在线变为离线。

2 个答案:

答案 0 :(得分:0)

绑定事件监听器以卸载并使用Navigator.sendBeacon发送请求,如下所示:

window.addEventListener("unload", yourFunction, false);

function yourFunction() {
    navigator.sendBeacon(url, data);
}

答案 1 :(得分:0)

如果要在Angular中实现

@HostListener('window:beforeunload', ['$event'])
handleBeforeUnload(event) {

//添加确认框并相应地放置代码 }

并实现ComponentCanDeactivate。

URL:-https://medium.com/better-programming/angular-how-keep-user-from-lost-his-data-by-accidentally-leaving-the-page-before-submit-4eeb74420f0d

希望这对您有所帮助。