First of all, I think you want details[i][2] in the loop, not details[i][3]. Second of all, the loop counter is incremented when your ajax call is made, but it doesn't print out the alert until after the call completes. In between those two different times, the counter is being incremented by the remaining passes of the loop.
It's printing out the CURRENT value of the loop counter, and in your case it's happening after the loop is done (hence i >= details.length). It's still iterating the correct number of times. 1st: 0 3rd: 0 1st: 1 3rd: 1 1st: 2 3rd: 2 1st: 3 3rd: 3 1st: 4 <-- called here GET http://blah 1st: 5 3rd: 5 1st: 6 3rd: 6 2nd: 7 <-- completed here On 9/25/2009 12:02 PM, Wade Preston Shearer wrote: > 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 > > > > > _______________________________________________ > > UPHPU mailing list > [email protected] > http://uphpu.org/mailman/listinfo/uphpu > IRC: #uphpu on irc.freenode.net _______________________________________________ UPHPU mailing list [email protected] http://uphpu.org/mailman/listinfo/uphpu IRC: #uphpu on irc.freenode.net
