Jump to content

AJAX in loops


rhyspaterson

Recommended Posts

Hey guys,

 

Having trouble using a counter in a for loop with AJAX calls. Code below:

 

for(var x = 0; x < allPrinters.length; x++){ 
		$.ajax( {
		   url : "/data/printer_data[x].xml", dataType : ($.browser.msie) ? "text" : "xml", success : function(data) {
			  var xml; if (typeof data == "string") {
				 xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.loadXML(data);
				 }
			  else {
				 xml = data;
				 }

				if(!xml.getElementsByTagName("array")){
					alert("NULL!");
				}

				// Returned data available in object "xml"
				var name = xml.getElementsByTagName("system.sysname.0")[0].childNodes[0].nodeValue;

				var type = xml.getElementsByTagName("25.3.2.1.3.1")[0].childNodes[0].nodeValue;
				type = removeDoubleQuotes(type);

				var consoleDisplay = xml.getElementsByTagName("43.16.5.1.2.1.1")[0].childNodes[0].nodeValue;
				consoleDisplay = removeDoubleQuotes(consoleDisplay);

					var row = document.createElement('TR');
					tmp.appendChild(row);

						var container = document.createElement('TD');
						// ----------------------------------------------------
						container.innerHTML= x;
						// ----------------------------------------------------
						row.appendChild(container);


						var container = document.createElement('TD');
						container.setAttribute('class', 'lft');
						container.innerHTML=name;
						row.appendChild(container);

						var container = document.createElement('TD');
						container.innerHTML=type;
						row.appendChild(container);	

						var container = document.createElement('TD');
						container.innerHTML=consoleDisplay;
						row.appendChild(container);											

						var container = document.createElement('TD');
						container.innerHTML='Undefined';
						row.appendChild(container);	

				document.getElementById('writeroot').appendChild(newTable);
				// Run the TableSort script
				fdTableSort.init('generatedTable');
			  }
		});	 						
	}

 

Say for example that allPrinters.length is 10.

 

I get the table rows generated the correct amount of times (10), however container.innerHTML= x is always set to 10 in each of the rows - instead of 1, 2, 3, 4 ect.. for each row generated. The x variable works for /data/printer_data(x).xml (i.e opening printer_data1.xml then printer_data2.xml ect)  but not for container.innerHTML= x.

 

Any suggestions?

Cheers,

Rhys

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.