1

I have an event that needs to indicate if the user is_online or not. I'm using the beforeunload event, which triggering when the page is closed, and when it trigger I put the is_online property to false

Its works good when I close the page directly. However, when I skip to another tab in my browser and wait couple of minutes and then I close the page, this event didn't triggered and my user is_online status still appears as true.

this is the relevant code:

window.addEventListener(
    "beforeunload",
    async (event) => {
      event.stopImmediatePropagation();
      await set_user_is_online({value:false});
    },
    { once: true }
  );
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Check out the `visibilitychange` event instead. See https://www.igvita.com/2015/11/20/dont-lose-user-and-app-state-use-page-visibility/ – benvc May 24 '22 at 21:10

0 Answers0