简体   繁体   中英

Check Pending AJAX requests or HTTP GET/POST request

How do i check if the page has pending AJAX or HTTP GET/POST requests? I use javascript and/or python for this checking.

what i wanted to do is execute a script if a page has finished all requests. onload doesn't work for me, if you used firebugs net panel, you would know. onload fires when the page is loaded but there is a possibility that there are still pending request hanging around somewhere.

thank you in advance.

figured it out. thanks for the effort guys. just plain and simple javascript.

interValRef = 0;

interValRef = setInterval("checkState();",100)

function checkState(){
    if(document.readyState == 'complete'){
        clearInterval(interValRef);
        myFunc();
    }
}

I see you mention you are using Prototype.js. You can track active requests with Prototype by checking the Ajax.activeRequestCount value. You could check this using setTimeout or setInterval to make sure that any requests triggered on page load have completed (if that's what you're looking to do)

I think you want to know if the HTML is fully loaded. In this case you can use the dom:loaded event. Here is an example on how to use it with prototype (but there must be a variant for other JS frameworks):

document.observe("dom:loaded", function() {
  // do whatever you want to do
});

This event will fire as soon as the DOM tree is loaded. So even before all the images or external data (including iframe) are loaded.

Here is the best way of checking it.

var loadingDone =  document.readyState=="complete" && jQuery.active === 0;

So loadingDone will be true, if Ajax calls are done. If its false then you can add waiting.

您需要跟踪每个XMLHttpRequest并监视它是完成还是执行异步回调。

Assuming you are using prototype.js you could keep track with a counter of all your request objects

var ACTIVE_REQUESTS = 0; // GLOBAL

ACTIVE_REQUESTS++
new Ajax.Request('/your/url', {
  onSuccess: function(response) {
    ACTIVE_REQUESTS--;
    // Handle the response content...
  }
}));

console.log("there are " + ACTIVE_REQUESTS + " open AJAX requests pending");
document.addEventListener("readystatechange", function(event) {
    if (document.readyState === "complete") {
        console.log("complete");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM