HELP - javascript loop reiterating too fast for ajax

I’m struggling with a call to a php script to save data to a mysql database. Basically I’ve created a loop to send (n) retrieved chunks of data to be saved. Each time only the last chunk is saved. However if I insert an alert() in onreadystatechange function I’m interrupting the script and all chunks get saved. Hence I deduct the loop is reiterating too quickly. Searching forums I’ve seen that by using setTimeout may help but not sure how to code this.

Can anyone help?

function saveFlights(){
var nb_grids = parseInt(document.getElementById(‘nb_grids’).value);
var direction = “out”;

for (i=1; i<=nb_grids; i++){

	var output = createUrlString(i, direction);				 
	if (xmlhttp){"GET","php_scripts/flights.php?"+output);
		xmlhttp.onreadystatechange = function(){

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				/*do something*/



function createUrlString(counter, direction){

/*script to produce output */


If you need to make a series of asynchronous requests, use the termination of the first to trigger the next and so on until there are no more to make.

Thanks. But as the number of requests is never a fixed number I can’t get my head around how to do this without a loop.

If you use Login Ali’s suggestion, and simply call the next iteration from the success callback of the previous iteration, it’ll be the same as looping indefinitely. You’ll probably need to keep some sort of counter of flag to store the status.

Perhaps using an array to store the things that needs to be queried, and simply popping the array every iteration until it’s empty?

I’ll try this, but as I’m very new to all this it may take some time to work out your suggestion. Here goes …

Looks like you need to being using separate xmlhttp request objects for each iteration.

For the moment there are none, I basically want to save the next set of data retrieved from the page to the database. For the moment if I have more than one only the last set of data is saved to the database.

Where and how is xmlhttp defined?

It’s defined at the beginning of the script:

//Browser Support Code

var xmlhttp = false;  // The variable that makes Ajax possible! 
	//If javascript version is greater than 5
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	//alert ("you are using IE");         
} catch (e){ 
    // If not, use the older active x object
	// If you are using IE
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		//alert ("you are using IE (2)");
    } catch (E) {            
		// Else we must be using a non-IE browser
		xmlhttp = false;
		//alert ("you are using a non-IE brower");

//If you are using a non-IE brower, create a javascript instace of the object
if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’){
xmlhttp = new XMLHttpRequest();
//alert (“You are not using Microsoft Internet Explorer”);

Well… than the first thing that needs to be done is create a function to return a xmlhttp request instance, rather than having a single global instance. This is where a library such as; jQuery would come in handy considering all the cross browser compatibility stuff is being taken care of for you. All you would need to do is use the method ajax with the proper configuration options. However, since your not using jQuery or some other library that provides a normalized xmlhttp request object you will need to create your own function to return one. The reason this will need to be done is that each requests needs to be represented with a separate xmlhttp object. Therefore, should be a single function that returns that object taking into consideration cross browser compatibility.

Thanks oddz, appreciate your help.

jQuery just might be little too much for me at the moment. I’ve created a function to return XMLHttpRequest objects. However I’ve used the global xmlhttp instance throughout my script. I can’t understand why doesn’t var xmlhttp=getHTTPObject(); create my global instance but instead gives me ‘xmlhttp is not defined’?

function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject(“Microsoft.XMLHTTP”);
return xhr;

var xmlhttp=getHTTPObject();