i am making a XMLHttpRequest open() call from a for loop.the loop iterates through set of links.I want to wait till the readyState becomes 4(the response comes) before going in to the next iteration.How can i do this?please help
Rather than using a for loop to control the iterations, set up an object that :
1) On creation is passed a 'nextIter' callback object
2) Makes it's own call
3) Then on readystate4 ...
3a) Does it's own processing
3b) Calls the member 'nextIter' function. This object would be another instance of the same object.
This way your for loop would just create an object structure where,
objA <whenreadystate4 refs> objB <whenreadystate4 refs> objC ...
... and the calls just chain up as each ajax call finishes, and delegates on to the next in the chain ... that is until the bottom element references null.
Hopefully that makes sense.
Btw my existing program looks like this and i want to wait till the readyState becomes 4 before going to the next iteration
onMenuItemCommand: function() { var i;//iteration value of the loop
var xhr=new XMLHttpRequest();//create the XMLHttp request
var e=content.document.getElementsByTagName('a');// get all the links in the content page
for( i=0;i<e.length;i++){ // loop goes through the selected links
webassist.setLink(e[i]); //set the link element
if(e[i]!=""){ // check the link's href value is null
xhr.open('HEAD',e[i] ,true);
xhr.send(null);
}
else{ //if the href value is null set it to color red
webassist.setColor("#FF0000");//red
}
xhr.onreadystatechange=function(evnt){
if(xhr.readyState==4){
if(xhr.status==200){
webassist.setColor("#99FF66");//green
}
else{
webassist.setColor("#FF0000");//red
}
} else{
webassist.setColor("#FF00FF"); //pink
}
}
}
}