我需要每隔 2 秒左右不断地敲击相同的 ajax 请求,直到收到响应。这是因为它被发送到的设备一次进入睡眠状态 3 秒——它需要在唤醒时捕获请求。有没有一种简单的方法可以通过在我的代码中添加一些东西来做到这一点?不使用jquery...
这就是我只发送一个请求所拥有的:
function ePOST(url, postData, callback) {
var postReq = AjaxRequest();
postReq.onreadystatechange = function() {
if (postReq.readyState == 4) {
if (postReq.error) {
callback(1, "Request had an error.");
alert('postReq Error');
} else {
var stat;
try {
stat = postReq.status;
} catch (err) {
callback(1, "Failed to get HTTP status from server.");
return;
}
if (stat == 200 || stat == 0) {
callback(0, postReq.responseText);
} else {
callback(1, "Unexpected HTTP Status: "
+ postReq.status);
alert('Unexpected HTTP Status: '
+ postReq.status);
}
}
}
}
if (postReq.overrideMimeType){
postReq.overrideMimeType("text/xml");
}
setTimeout('',1000);
postReq.open("POST", url, true);
postReq.send(postData);
return postReq;
}