How can I call my function: myFunc1
in jquery
after the browser has completely sent an ajax request?
I want to run a callback function after a jquery ajax has been sent. I don't want to do this on complete
or success
because I don't want to wait for the request to complete. I want to fire it right after the ajax request has been sent from the browser.
This is use case for which i need this:
I want run a function after making an ajax request. My original code was this:
$.ajax({url: 'x.html'});
myFunc1();
In the case when `myFunc1() changes the browsers location, the browser would cancel the pending ajax request.
My current solution is to do this:
$.ajax({url: 'x.html', complete: myFunc1});
This makes sure that myFunc1
is not called till the ajax is complete. Although this solution works, it is inefficient because myFunc1
wont run till the request is complete. Since I don't care what the ajax request returns, whether it fails or succeeds, I dont need to wait for it to complete to run the myFunc1
function.
This is what i did to run my callback function sufficiently after the AJAX call had been made so the browser doesn't cancel it and without having to wait for the server to complete the resquest:
Here the timeout makes sure that if my server doesn't repond in
100ms
, thecallback
is executed.100ms
also is sufficient time for the browser to send the request.I experimented with status codes and finally ended up not using them. I found them to unreliable cross browser and for JSONP requests missing.