Using jQuery, how can I cancel/abort an Ajax request that I have not yet received the response from?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
Just call xhr.abort() whether it's jquery ajax object or native XMLHTTPRequest object.
example:
I have shared a demo that demonstrates how to cancel an AJAX request-- if data is not returned from the server within a predefined wait time.
HTML :
JS CODE:
If
xhr.abort();
causes page reload,Then you can set
onreadystatechange
before abort to prevent:Just use ajax.abort() for example you could abort any pending ajax request before sending another one like this
We just had to work around this problem and tested three different approaches.
In our case we decided to use approach #3 as it produces less load for the server. But I am not 100% sure if jQuery guarantees the call of the .complete()-method, this could produce a deadlock situation. In our tests we could not reproduce such a situation.
I was doing a live search solution and needed to cancel pending requests that may have taken longer than the latest/most current request.
In my case I used something like this: