|
XMLHttpRequest not working in Loop
Try to load details from sever side based on the provided prarams which are store in an array. Thus, there will be a loop, sending different request with different array element value. and I will use the return values (req.responseText) to replace some parts of the page.
The problem is that only the last request will be given the correct value. I tried to add "alert('');" to find out the problem, interesting thing is, if I close the alert window a little big slowly, all requests will be returned with correct values. However, if close them very quickly, only the last request working.
I thought it might be the reason that XMLHttpRequest need more time to process, and try to add setTimeout, not lucky, the same.
Below are main processing codes, please advise, thanks!
// loadParams has been defined by the array before it is sent here
//alert(loadParams );
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState==4)
if (req.status==200)
//do innerHTML replace
}
req.open("GET", loadParams, true);
req.setRequestHeader("Content-Type","text/html;charset=utf-8");
req.send(null);
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function(){
if (req.readyState==4)
if (req.status==200)
//do innerHTML replace
}
req.open("GET", loadParams, true);
req.setRequestHeader("Content-Type","text/html;charset=utf-8");
req.send();
}
}
|