您好,我目前正在发出 xmhhttp 请求,但该网站需要一些时间来加载,所以我只得到 ReadyState = 3 和 status = 200。所以我需要一些东西来等待 readystate = 4,但我想限制如果 readystate = 4,此函数仅每秒检查一次,否则什么都不做。
这样的延迟函数长什么样子?
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);
}
最佳答案
你为什么要发明轮子?
您应该只将处理程序传递给 XHRs
onreadystatechange
回调。
xmlhttp.onreadystatechange = function() {
switch( xmlhttp.readyState ) {
case 4: {
if (xmlhttp.status === 200) {
var txt = xmlhttp.responseText; // or do something else here
}
break;
}
case 3: {
// go interactive !
break;
}
}
};
关于javascript - 需要一个延迟函数javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864471/