问题
I'm trying to call a method in the beforeunload event handler. It seems like the browser is closing too fast. Here is my code:
@HostListener('window:beforeunload', ['$event'])
onBeforeUnload(): boolean {
let logout: any;
logout = logout();
while (logout = false) {
console.log("session not ended")
}
return true;
}
public async logout(): Promise<boolean> {
this.userSessionService.EndSession(this.userSessionId);
this.oauthService.logOut(false);
return true;
}
What I want to happen is when a user closes the browser, the logout method is called so that my database is updated.
来源:https://stackoverflow.com/questions/62030052/onbeforeunload-doesnt-call-methods-browser-closes-too-fast