Often I have a situation where an action on the page causes an ajax request. When several of these actions have stacked up, each ajax request in 开发者_如何学Goturn comes in and does its action (maybe updating a list of items). This looks horrible. I'm trying to think of a way to have a request only update the list of items if no requests for the same type of thing are currently "pending". Ideas?
Your options:
Keep a list of request and check if the element is in the list. If it is in the list it means the request is happening. If it is not, you can start a request and add it to a list. Example on jsFiddle.
Before making a new request abort the existing one. This will cause the last request to go and others will be discarded, opposite from solution 1. Example on jsFiddle.
Keep a list of requests. When you submit the request, add it to the list. On your load and error functions, remove it from the list.
Therefore, when you process the load, if there are no requests on the stack, then it will process, or else it will return null.
var doing_ajax = false;
$("#action").click(function(){
if (!doing_ajax){
doing_ajax = true;
do_ajax();
}
});
function do_ajax(){
$("#content").load('something.php', function(){
doing_ajax = false;
});
}
精彩评论