Consider the following code:
var x = 0;
do {
$.ajax({
type: "POST",
url: someurl,
dataType: 'xml',
data: xmlString,
success: function(xml) {
x++;
}
} while(x<10);
This would, of course, not work because thousands requests would be made to “someurl”, so how would I go about changing this code in order to have the do while condition depend on the callback of the asynchronous HTTP request?
In other words, how would I have the do-while loop continue only upon calling the request callback? So that only 11 iterations will be made, not thousands.
You could re-write it like this:
This way, the method calls itself when it succeeds ten times, then calls the ‘after’ method to continue with your other code.