我想知道,假设您对运行PHP代码的页面进行了ajax请求。页面输出一些数据(使用flush()方法或其他方法),但由于30秒超时或其他错误,php请求结束。
现在当请求结束时,我想在客户端找到它并重新启动请求。
。假设我有这样的东西
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==3 && xmlhttp.status==200){
document.getElementById("A").innerHTML=xmlhttp.responseText;
}
else if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("A").innerHTML=xmlhttp.responseText;
//plus some additional code to end the process
}
else if(xmlhttp.status== SOMETHING /*WHAT DO I ADD HERE TO KNOW THAT THE SERVER HAS TIMED OUT OR SOMETHING SO I CAN RESTART THE XMLHTTP CYCLE */){
//code to resend xmlhttp request.
}
}
我使用的一个策略是在JS中设置一个计时器,然后在调用成功时清除它。
var myTimer = setTimeout(stuffToDoOnFailure, 6000); // 6 secs
ajaxCall(function callBack() {
clearTimeout(myTimer);
});
编辑:当然,如果ajax调用在6秒后成功,您可能会同时执行stuffToDoOnFailure
和callBack
,因此您希望以某种方式处理这种情况。不过这取决于你的应用。