for loop which makes ajax calls

for loop which makes ajax calls

hi.

i have a for loop which (upon every iteration) calls the function $.getJSON(....) which gets data from an external php file and populates a table in my document using the .appendTo() function. the ORDER in which the data is retrieved is very important for my application.

document:

>> data from $.getJSON(..0..)
>> data from $.getJSON(..1..)
>> data from $.getJSON(..2..)
>> data from $.getJSON(..3..)

where the 0,1,2,3 are values passed from the for loop to get appropriate data which DEPENDS on the index value that is passed. hence, the ORDER in which the data is retrieved is very important for my application.

the problem i am having is that as my app goes through this for loop, i dont know why but the output data is displayed in an inconsistent order. for example, sometimes it is:

>> data from $.getJSON(..0..)
>> data from $.getJSON(..1..)
>> data from $.getJSON(..3..)
>> data from $.getJSON(..2..)

(it is inconsistent, sometimes it is the correct order)

what is the reason for this inconsistency and how do i resolve it? are some $.getJSON(....) calls finishing faster than others that despite the loop, some of the data is being loaded into the document in non-sequential order making the appendTo() function output inconsistent orders?

my attempts to debug:
i put an alert before the $.getJSON(...) call which showed that value that was being passed to the function so i got alerts at every iteration in the for loop. with the alert in place, the correct order was shown EVERY TIME =D. but without the alert, it was inconsistent. does this support my original hypothesis that it has to do with the time taken to load the data for each $.getJSON(...) call? if i put an alert in place, it provides taht time buffer needed to load the data upon every iteration before proceeding to the next... obviously, i don't want to show alerts to my users when they are using this feature...

any help would be greatly appreciated! Thanks! i would be happy to provide code if there is interest but the above is the general overview of what my code does.