[UPHPU] ajax causing my loop to loop more than allowed

Wade Preston Shearer wadeshearer.lists at me.com
Fri Sep 25 12:02:43 MDT 2009


Consider the following javascript code:

details = new Array();

details[0] = new Array('pig', 'cow');
details[1] = new Array('pig', 'cow');
details[2] = new Array('pig', 'cow');
details[3] = new Array('pig', 'cow');
details[4] = new Array('pig', 'cow', 'ajax');
details[5] = new Array('pig', 'cow');
details[6] = new Array('pig', 'cow');

for(var i = 0; i < details.length; i++) {	
	// first alert
	alert(i);
	
	if(details[i][3] == 'ajax') {
		$.get('core/data.php', {data: column}, function(data) {
			load_switch(data);
			
			// second alert
			alert(i);
		});
	} else {
		var data = $('.data tbody tr:eq(' + row + ') .' + column).text();
		load_switch(data);
		
		// second alert
		alert(i);
	}
}


Alert 1 produces: 0,1,2,3,4,5,6
Alert 2 produces: 0,1,2,3,5,6,7


I can understand alert 2 being out of order or incorrect since the  
loop continues to spin while the ajax call is made, but I don't  
understand how the counter in the loop ever makes it to 7. Assuming  
that the loop finishes cycling before the ajax call finishes,  
shouldn't alert 2 produce this?

0,1,2,3,5,6,6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2431 bytes
Desc: not available
Url : http://uphpu.org/pipermail/uphpu/attachments/20090925/bd4305da/attachment.bin 


More information about the UPHPU mailing list