0

I'm doing a simple jQuery get like below which works fine

$.get("http://test.example.com/do-something", function (data) {
    console.log("test work done");
});

GET request is started and user experience is not disturbed. User is able to interact with the page.

BUT, if user tries to reload the page while the GET request hasn't finished, the browser will have him waiting until is done.

This is my actual question: how to have the browser NOT to wait for the request launched to finish or to time out?

Bogdan
  • 1,840
  • 1
  • 25
  • 39
  • 2
    Not sure how an asynchronous request causes that.... My guess is it is not the asynchrous request, it is the code inside or jQuery processing a large data (Json parse, etc) – epascarello Jul 21 '17 at 12:59
  • 1
    Uh? It doesn't. It cancels network requests and reloads the page immediately. – Jeremy Thille Jul 21 '17 at 13:00
  • @epascarello - give it a try - the get request MUST be towards the same website your page is running on – Bogdan Jul 21 '17 at 13:01
  • I have a page with multiple Ajax requests firing off and do not have issues. Do you have a demo page that demonstrates this? – epascarello Jul 21 '17 at 13:02
  • Is it possible that the server process serving the `$.get()` is not handling the cancellation of the request well? – Justin Pearce Jul 21 '17 at 13:04

2 Answers2

1

Use the .abort() and .unload() functions in jQuery. Here is an example

var t = $.get(url, function(){
    //do things here
})

$(window).unload(function() {
    t.abort();
})
CumminUp07
  • 1,936
  • 1
  • 8
  • 21
0

You could store the result of get in a variable and use the abort() method to cancel it when certain events are triggered. The behavior you are describing is not expected though.

Jean Pacher
  • 327
  • 4
  • 11