admin管理员组

文章数量:1291089

How to know when all ajax calls are plete?,now I just use a delay timeout to wait the request plete,these is a better way?i want make sure ajax requests are pleted...

How to know when all ajax calls are plete?,now I just use a delay timeout to wait the request plete,these is a better way?i want make sure ajax requests are pleted...

Share Improve this question edited May 7, 2012 at 9:32 gdoron 150k59 gold badges302 silver badges371 bronze badges asked May 7, 2012 at 9:25 Zhen ZhangzhenZhen Zhangzhen 411 gold badge2 silver badges6 bronze badges 3
  • 3 Are you using a framework/library to make your Ajax requests? Like jQuery or prototype or anything similair? – w00 Commented May 7, 2012 at 9:27
  • You should have included the code that send the ajax request... – gdoron Commented May 7, 2012 at 9:34
  • In your title you ask how to know when one Ajax request is pleted, in the body you ask about all Ajax calls. That's a difference, what do you want to know? – Felix Kling Commented May 7, 2012 at 9:39
Add a ment  | 

6 Answers 6

Reset to default 4

The XMLHttpRequest object has readyState property and onreadystatechange that you can manipulate like that:

var xmlhttp;
if (window.XMLHttpRequest) { 
    xmlhttp = new XMLHttpRequest();
}
else { 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) { // xmlhttp.status==200 == successful request
        // What you want here
    }
}
xmlhttp.open("GET", "foo_url.php", true);
xmlhttp.send();
}​

You can do it simpler with jQuery, if you're using that library:

$.ajax({
    url: 'foo_url.php',
    plete: function() {
        // What you want here
    }
});

If you're using jQuery's AJAX, it has a plete option, see docs

$.ajax({
    url: 'foo.php',
    plete: function(jqXHR, textStatus) {
        alert('AJAX call plete');
    }
});
   xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    // This is when your Ajax request is plete 
                }
            }

that is depend on your library; ex, if you are using jquery then you can use success: parameter and if javascript then you can use if (xmlhttp.readyState==4 && xmlhttp.status==200) statement.

If you are using xmlhttp object for ajax call then when xmlhttp.readyState==4 then it is consider as ajax request is pleted.

$.ajax({
url: 'abc.php',
data: "&id=" + id ;
plete: function(data) {
    alert(data.responseText);
}
});

本文标签: javascriptHow do I know when an ajax request is completedStack Overflow