您好,我目前正在发出 xmhhttp 请求,但该网站需要一些时间来加载,所以我只得到 ReadyState = 3 和状态 = 200.所以我需要等到 readystate = 4 的东西,但我想限制如果readystate = 4,它每秒只检查一次,否则什么也不做.
Hi im currently making a xmhhttp request, but the site takes some time to load, so I only get the ReadyState = 3 and status = 200. So I need something that waits until the readystate = 4, but I want to limit this function so that it only checks once a second if the readystate = 4, else do nothing.
这样的延迟函数长什么样子?
How can such a delay function look like?
if (xmlhttp.readyState==4 && xmlhttp.status==200)//Add the delay here so that the else doesn't occur
{
var txt=xmlhttp.responseText;
.....
else {
document.write("status: " + xmlhttp.readyState + " " + xmlhttp.status);
}
我们可以写一个函数来检查你的 xmlhttp-object 的状态:
We can write a function for checking the state of your xmlhttp-object:
var checkState = function(xmlhttp, callback) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(whatever, arguments, that, you, want, to, send);
} else {
// Check back again 1 sec later
setTimeout(checkState, 1000);
}
};
那么你可以这样使用它:
Then you can use it like this:
checkState(xmlhttp, function(whatever, arguments, you, need) {
// the code here will be run when the readyState is 4 and the status is 200
});
不过有两件事:
这篇关于需要一个延迟函数javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)