javascript 检查挂起的 AJAX 请求或 HTTP GET/POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3262473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:40:45  来源:igfitidea点击:

Check Pending AJAX requests or HTTP GET/POST request

javascriptpythonhtml

提问by klambiguit

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

如何检查页面是否有待处理的 AJAX 或 HTTP GET/POST 请求?我使用 javascript 和/或 python 进行此检查。

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.

我想做的是在页面完成所有请求时执行脚本。onload 对我不起作用,如果您使用过萤火虫网络面板,您就会知道。onload 在页面加载时触发,但仍有可能在某处挂起待处理的请求。

thank you in advance.

先感谢您。

采纳答案by klambiguit

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

弄清楚了。感谢你们的努力。只是简单明了的javascript。

interValRef = 0;

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

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

回答by robjmills

I see you mention you are using Prototype.js. You can track active requests with Prototype by checking the Ajax.activeRequestCountvalue. 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)

我看到你提到你正在使用 Prototype.js。您可以通过检查Ajax.activeRequestCount值来使用 Prototype 跟踪活动请求。您可以使用 setTimeout 或 setInterval 检查这一点,以确保在页面加载时触发的任何请求都已完成(如果这是您要执行的操作)

回答by user8462556

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.

因此,如果 Ajax 调用完成,loadingDone 将为 true。如果它是假的,那么你可以添加等待。

回答by Alexey Bulash

document.addEventListener("readystatechange", function(event) {
    if (document.readyState === "complete") {
        console.log("complete");
    }
});

回答by 2ndkauboy

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

我想您想知道 HTML 是否已完全加载。在这种情况下,您可以使用dom:loaded事件。这是一个关于如何将它与原型一起使用的示例(但必须有其他 JS 框架的变体):

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.

这个事件会在 DOM 树加载后立即触发。因此,即使在加载所有图像或外部数据(包括 iframe)之前。

回答by John Weldon

You would need to keep track of each XMLHttpRequest and monitor whether it completes or the asynchronous callback is executed.

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

回答by David W. Keith

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

假设您正在使用prototype.js,您可以跟踪所有请求对象的计数器

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");