Implement multiple AJAX requests with closures to execute only the last time

Source: Internet
Author: User

Click a button, then request resources to the server, without processing, many times there will be a number of requests waiting. We know that in general we use AJAX is asynchronous request, then we quickly repeated click on a button to get the results we do not know which is the result of the click may be the first time may be the last time may be the second time. So how do we solve this problem, the simple brute method is to send the request when the button disabled after the request is completed successfully disabled, then once the request failed request will always be in pending state, so consider the situation is more success,error , complete and other events, and is not conducive to user experience. The Abort method of Ajax can solve this problem very well, but use this abort (), when the method cancels, it actually triggers the $.ajax success event, so the callback function needs to be added to determine if the AJAX request exists before the success is executed.

Our project on the use of Ajax is a well-encapsulated method, when using the Abort method to consider the possibility of multiple requests at the same time, then we in the encapsulated Ajax method there is no way to use the Abort method, Because a unified approach can only execute a single request (which is, of course, the way we do it in our project), you can think about it in terms of your own encapsulated Ajax approach.

In resolving the problem of duplicate requests we have adopted a single problem solving method (which may not be the best method to be optimized ~)

1. Define a global variable var flag=0//is used to record the number of clicks;

2. click Method

function Requestevent () {

flag++;

...

Callback functions for AJAX requests

Callbackfun: (

function (a) {

if (A<flag) {

Return

}//determines whether the current callback function is the last request

CB (RES);//function method to perform callback success

}

) (flag)

}

There is no cancellation of the request, but only the last request of the callback function, a dish ~ everyone has a good way to welcome the exchange

Implement multiple AJAX requests with closures to execute only the last time

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: info-contact@alibabacloud.com 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.