0

When browser window is closed all resources acquired by the software is released instantly. So when I close the window, logically there should not be any process running. So how exactly is the function bound to beforeunload executes. Moreover what if there is an AJAX call to server, how does it guarantee that the server database will write my contents that I am passing through this function.

window.addEventListener("beforeunload", function (e) {
  // AJAX CALLto the server
  (e || window.event).returnValue = null;
  return null;
});

How is that AJAX call executed. My browser gets closed in not even a second. What if I want to write to the database, will this AJAX call execute successfully if yes then how is it implemented internally.

EDIT : Someone in comments pointed out that there is no guarantee of that AJAX call. So that means there is a chance. If there is a chance then how that process is executed. And also what happens if we close the tab. Is it still 100% guaranteed. If yes then why and how?

Dharman
  • 26,923
  • 21
  • 73
  • 125
Md Imran
  • 61
  • 1
  • 14
  • There is no guarantee the Ajax call will make it to the server. Years ago, there was ways to force it, but most modern browsers kill the http requests that are open. – epascarello Oct 24 '19 at 13:18
  • So whats the best way to make that call happen – Md Imran Oct 24 '19 at 13:19
  • Nothing happens "instantly". It's run because the browser sends that event and runs any attached functions before the window is destroyed--the same way any code works. If there's something asynchronous there *is* no guarantee. If you want something to complete before the function ends then it must be a synchronous call. – Dave Newton Oct 24 '19 at 13:22
  • @DaveNewton can you point me to the right source to how to make my call synchronous – Md Imran Oct 24 '19 at 13:24
  • @FarhanAhmed I'd probably just search the web--my ESPN is out and I can't determine how you're making your Ajax call. And if I could, I'd point you to the docs of whatever you're using anyway. – Dave Newton Oct 24 '19 at 13:31
  • Just use session timeout like it is meant to be used. Or websockets which knows when it is cut off. – epascarello Oct 24 '19 at 13:31
  • @epascarello what happens if we close the tab. Is it still 100% guaranteed. If yes then why and how? – Md Imran Oct 24 '19 at 13:40
  • There is no 100% guarantee it will fire or make it. What you want to do is not going to work 100% of the time. That is why other solutions are better. – epascarello Oct 24 '19 at 14:02
  • @epascarello I want to store the last position where the video is currently running in the user page and the next time when user logs in. I want the video to be started from the same point. Could you please suggest me how can I do this efficiently? – Md Imran Oct 24 '19 at 14:16
  • Store it in local storage and make the Ajax call, if the Ajax call fails, at least you have localstorage to fall back on. – epascarello Oct 24 '19 at 14:18
  • @epascarello I didn't get it can you please elaborate. I mean once the page is closed what good will that localStorage do to me – Md Imran Oct 24 '19 at 14:19
  • next time they load the page in that browser it will be there... yes it will not work if they are on a different device – epascarello Oct 24 '19 at 14:27
  • @epascarello can you suggest me some other better way, how netflix, coursera, amazon prime have implemented. Just tell me the logic – Md Imran Oct 24 '19 at 14:29
  • localStorage data remain persistent until explicitly deleted. When the page is loaded again, you can check for the existence of your data and act accordingly. – Wizard Oct 24 '19 at 14:39
  • I can bet they are not using Ajax requests – epascarello Oct 24 '19 at 14:40
  • https://stackoverflow.com/questions/10882012/is-it-possible-to-save-all-field-information-when-the-user-closes-his-browser – Wizard Oct 24 '19 at 15:47
  • @Wizard thanks, I think polling should be a good idea over here. At every 5 second I can save(overwrite) the video currenttime in the uservideo database(using ajax) and fetch from it everytime user plays the video again. What do you think? – Md Imran Oct 24 '19 at 16:36
  • Try localStorage with onunload event. – Wizard Oct 24 '19 at 17:26
  • Synchronous code inside the `beforeunload` will run to completion but async code most likely will not complete. For ajax you can _force_ it to be synchronous by using `async: false`. I have found this case to be basically the _only_ legitimate time to use `async:false` and even then I cringe at it. – nurdyguy Oct 24 '19 at 19:40

0 Answers0