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