This question is like another, except that one is asked in the context of JQuery, which I don't use.
Ajax code on my page issues a POST every ten seconds. Once in a while -- every ~600 requests -- my client code hangs waiting for a response that will never show up.
The code will time out and re-issue the Ajax request, but first I want to clean up everything about the outstanding request: I don't want to leave any vestigial state around to muck things up later.
What is the right way to cancel the outstanding request so that things get properly tidied up?
Thanks!
Sometimes you might want to cancel the ajax request that is pending from long time or want to cancel request if user accidently clicked submit button. In that cases, so you can just use abort() method. If the request has been sent already, this method will abort the request.
abort() Method: This method is used to abort or cancel the HTTP request.
Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.
Store a reference to your last AJAX request and abort it when you start the next one.
For instance:
var lastRequest;
setInterval(function() {
lastRequest.abort(); // abort the last request
lastRequest = new XMLHTTPRequest(); // and the rest of your XHR code
}, 10000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With