Ιs there any convenient way to make XMLHTTP Request in Javascript? Wait for example 3 seconds before sending? I have an array full names
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
}
And for each of them I want to receive JSON response from a server, but it bans me for sometime if i send requests too often, so I need to send them like every 3 seconds, wait for response, process the response and start a new one.
I guess I'll have to make it synchronous, so I already put there false argument in xhr.open
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…