In this coding example the function logout()
won't execute all it's async calls and won't wait till they are finished – instead page is unloading before, because the return of the beforeunload
event triggers unloading the page.
$(window).on('beforeunload', function(event) {
event.preventDefault();
logout();
return;
});
What I want to try is that the event function returns AFTER several asynchronous calls in logout()
are finished.
EDIT: My goal is NOT to show an alert with this! I only want to execute some code before the page is unloaded. The logout function could contain ajax requests and jQuery animations with some duration that needs to be finished.
I tried with callbacks, but end up with this, what isn't the desired result since it's returning to the callback, not to the event function.
$(window).on('beforeunload', function(event) {
event.preventDefault();
logout(function(x) { return; });
});