Call API on tab close/browser close in Angular

642 Views Asked by At

I'm trying to invoke an API call as soon as the tab or browser closes. For this I'm using 'onBeforeUnload' event. Currently, as soon as the browser is closed, the API is not getting called always. It works 2-3 times out of 10 times. Is there a way where I can ensure that the API gets called every time.

1

There are 1 best solutions below

1
Sunil Soni On

Can you add more details about your requirement, as I do not recommend this approach you are using. As it is very unsafe to use you cannot ensure it will always run as there are many scenario involved like process termination, power cut of local pc, internet disconnected and many more.

In case you only want to detect user is alive or working on your application you can create a Heartbeat kind of api that will tell the system that user is active and when server did not receive heartbeat for a defined time you can consider that as closing event.

if you can post the what you want to achieve people will defiantly help.