2

I want to update status flag 1 if user working on say 'XYZ' page and immediately update 0 when user leaves this page

i can able to update flag 1 by simple update query with linq in respective controller when user enters in.

but the problem is how to update flag 0 when user leaves or closes browser window directly

i tried adding following code :

window.onload = function () {
    window.addEventListener("beforeunload", function (e) {
        ----------AJAX FUNCTION TO UPDATE DATA HERE--------------
    });
};

But the problem is how to get callback of this alert-box see the image here

devSS
  • 39
  • 2
  • 7

1 Answers1

0

You can't be certain that this will work consistently just by using clientside functionality, as there could be many ways a session ends, which would NOT be caught by this. Examples could be:

  • losing internet connection
  • losing power
  • server endpoint being down

If you want consistency in this, you have to back it up with something server-side, that "expires" sessions that have not been heard from in x minutes.

So my suggestion:

  1. Set flag to 1 when user enters page.
  2. Implement the code you already have to update flag to 0.
  3. Run a server function (using hangfire for background jobs could be an option) and update all sessions with flag 1 to 0 when it has not been active for x minutes.
Kjensen
  • 12,447
  • 36
  • 109
  • 171
  • is there any chance to get that event when user leave the page instead of setting fix time – devSS Nov 06 '19 at 10:45
  • You are on the right track with beforeunload, if that is what you mean. See an example here: https://stackoverflow.com/questions/1704533/intercept-page-exit-event – Kjensen Nov 06 '19 at 16:06