0

This code is correct work if user close one or two tabs of browser

@HostListener('window:beforeunload')
  async ngOnDestroy() {
    if (this.myValueSub) {
        this.myValueSub.unsubscribe();
    }

    await this.authService.logout();
  }

But this code not work if user close multiple browser tabs at once (five tabs or more)

What is the problem?

Can't determine the cause of the problem

JactDotNet
  • 21
  • 1
  • Not sure of the exact answer so I won't post it as one, but my first thought is that browsers (chrome, at least) put unused tabs to sleep in some fashion. What exactly that means is up to some Googling that I'm not interested in doing. Maybe it's that? Sleepy tabs don't run their JS so don't run their unloading code? – Krenom Jan 13 '23 at 08:03
  • 1
    If you are experiencing an issue where the ngOnDestroy method is not being called when closing multiple browser tabs at once, it could be because the browser is terminating the JavaScript execution before Angular has a chance to clean up the resources. This could also depend on the specific browser you are using as some browser handles the closing of multiple tabs differently. To mitigate this issue, you can consider using the beforeunload event to explicitly clean up resources before the tab is closed. – Vijay Prajapati Jan 13 '23 at 09:23
  • maybe an simple example with beforeunload ? – JactDotNet Jan 13 '23 at 15:37

0 Answers0