onBeforeUnload doesn't call methods, browser closes too fast?

我只是一个虾纸丫 提交于 2020-06-01 07:40:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!