How to terminate JQUERY's $. AJAX request

Source: Internet
Author: User
This article will share with you the use of abort to terminate JQUERY's $. for more information about the AJAX request methods and examples, see the following:

1. if you click 5 ajax requests in a row, the first 4 requests are actually invalid, saving resources as early as possible.

2. the more serious problem is that the response to the last request may not be the last one, which may cause confusion. A queue is also required to maintain the sent request and response.

In fact, I have already designed the implementation method of this queue. later I found that jQuery directly uses the abort method, so that it does not need to be so complicated. after all, there are other tasks waiting to be completed.

It is really convenient to send ajax requests with jquery, such as $. get, $. post, and $. ajax, but sometimes we need to abort ajax requests halfway.

For example, when using comet for chatting, after sending a request, the server will refresh the link and return data after dozens of seconds. Assume that the server refreshes the link once every 30 seconds. what if we want to stop this ajax request in 10 seconds?

Add the code first, and then explain it later.

Var ajaxGet = $. get ("comet_server.php", {id: 1}, function (data ){.... // Some operations}); ajaxGet. abort ();

The above code has two knowledge points:

1. the data type returned by $. get is XMLHttpRequest. please refer to the manual. ($. Post, $. ajax, $. getJSON, and $. getScript are the same)

2. the XMLHttpRequest object has the abort () method.

Note: After abort (), the ajax request is immediately stopped, but the subsequent function () will still be executed (). If you want to avoid executing the operation, you can add a judgment at the start of function ().

Var ajaxGet = $. get ("comet_server.php", {id: 1}, function (data) {if (data. length = 0) return true ;.... // Some operations}); ajaxGet. abort ();

Terminate an ajax request:

Var request = $. get ("ajax. aspx ", {id: 1}, function (data) {// do something}); // terminate the request action. request. abort ();

Prevent repeated requests:

Var request; if (request! = Null) request. abort (); request = $. get ("ajax. aspx ", {id: 1}, function () {// do something}); ajax & setTimeout implements secondTry to terminate firstTry ajax after waiting for one second: var firstTry = $. ajax (// do something); var secondTry = setTimeout (function () {alert ('OK'); firstTry. abort ()}, 1000 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.