Ι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
.
H i friend,
I just saw your post and I understand that you want to do a request queue - send the first request after 3 seconds and wait it to complete then send next and next till the end of the queue.
I made a very simple class
Request
andRequestManager
which will do this for you.Have a look on the code and let me know if something is unclear to you. Try to read the comments also.
The sample code snippet is sending requests to Wikipedia to demonstrate that they are succeeding because your links are not working ones.