2

I've this piece of code

    window.onbeforeunload = function (evt) {
        //angular $http object
        Rest.putInteraction({id:1}).then(function(){
            //authorize the event to close window
        });        
    };

Javascript of course doesn't wait for the rest call to start, and simply let the page unload at the end of the callback; so I cannot make the request I need beofre the page get unloaded.

Is there any way to pause the event callback and wait for the rest promise to resolve?

Nemus
  • 1,322
  • 2
  • 23
  • 47
  • Can you please create a [jsfiddle](https://jsfiddle.net) with your code and the relevant issue? – Noy Feb 18 '16 at 10:25
  • 1
    Use a synchronous request instead – Nadir Feb 18 '16 at 10:30
  • As the [onbeforeunload](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload) documentation, the only thing you can do is to show a confirmation message; check also http://stackoverflow.com/questions/821011/how-do-you-prevent-a-webpage-from-navigating-away-in-javascript – beaver Feb 18 '16 at 11:11
  • @Nadir yes! That's the only solution – Nemus Feb 18 '16 at 15:21

1 Answers1

0

To wait until the call has been satisfied the only way I found is to make a synchronous call:

window.onbeforeunload = function (evt) {
     //jQuery Ajax
     $.ajax({
          async: false,
          method: "GET", 
          url: "http://www.google.it"
     });
     //here only after everything are done!
};
Nemus
  • 1,322
  • 2
  • 23
  • 47