Reputation: 43
My requirement is to loop over(keep reinvoking) the request after i get the response from my existing xhr request. I am currently setting a timeout for 2 seconds.But I am still not sure if this works. I don't want a strict timeout as the request may take a while sometimes which is okay(as it has a db call). So any suggestions out there how i can call the function again after i get a response?
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false);
function htmlGen (log){
var data_count = someNumber;
var url = someURL;
request.open("GET", url, true);
request.timeout = 2000;
request.onreadystatechange = updatePage; //some function to update my page
request.send(null);
}
function timerCntrl(command,log) {
if (command == "start") {
document.getElementById("start").disabled = true;
document.getElementById("stop").disabled = false;
timer = setInterval(function() { htmlGen(log);}, 2000);
}else{
document.getElementById("watchStart").disabled = false;
document.getElementById("watchStop").disabled = true;
clearTimeout(timer);
}
}
Thanks in advance :)
Upvotes: 2
Views: 78
Reputation: 6597
This code should work. It makes a request and then waits for its response. As soon as the response arrives, it makes another request and then waits for its response... And so on...
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false);
var url;
function updatePage();
async function startRequests() {
while(true) {
request.open("GET", url, false);
request.onreadystatechange = updatePage; //some function to update my page
request.send(null);
}
}
Note that for some reason these don't get logged on the console (witch funny enough made me have to deal with a "Too Many Requests" error in SO). Also, this is really not a good thing. This is more like a DDoS than an actual HTTP request. Be careful using this.
Upvotes: 3