简体   繁体   中英

How can I wait browser to close till server call gets complete?

I'm trying to call one service which updates data on browser close or tab close. The problem is that service call using $http and its async so according to me browser unload event cancels that call and close the browser. I want browser to wait until I get response/error for that service call.

I tried calling e.preventDefault() and e.stopPropagation() but its not working.

function releaseDocument(e) {
    EventService.releaseDocument().then(function() {
        window.close();
    }, function() {
        window.close();
    });
}

$window.onbeforeunload = releaseDocument;
$window.onunload = releaseDocument;

Can anyone help or suggest any alternatives to achieve the same!?

May be we can do it by using synchronous AJAX call, and also It's my suggestion only.


We will show a confirmation box when you close browser/tab. at this same time we will call your service synchronously . But we should call this service before execute the confirm box line. So sync call will execute every line and return the result then only the confirmation will fire/ or close the tab. Si here we have few time to execute the function.


 window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "Do you want close this browser";    
   releaseDocument()// it should be an sync function ( like you can set async:false in your ajax options 

  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM