0
是否有任何方便的方法来制作XMLHTTP请求Javascript?例如发送前等待3秒钟?我有一个数组的全名XMLHTTP请求的延迟
var items = [
{ url: "www.google.com/getChocolate", name: "Chocholate"},
{ url: "www.google.com/getCake", name: "Cake"},
{ url: "www.google.com/getCookie", name: "Cookie"},
];
for (var i = 0; i < items.length; i++) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data;
data = JSON.parse(xhr.responseText);
// processing data here
}
};
xhr.open("GET", items[i].url, false);
xhr.send();
// I somehow have to force to send the request for each item only every 3 seconds
}
并为他们每个人我想接收来自服务器的JSON响应,但它禁止我的某个时候,如果我过于频繁发送请求,所以我需要给他们像每3秒,等待响应,处理响应并开始新的响应。 我想我必须使它同步,所以我已经在xhr.open
中存在错误的参数。
'setTimeout'就是你要找的东西。 –
*“我必须让它同步”* ......永远不要这么做!它阻止了整个用户界面,因为这是一个糟糕的做法而被弃用 – charlietfl
如果我在循环中为请求添加'setTimeout',它会等待给定的时间并一次发送所有项目的请求,我需要它们发送一个一。 –