I'm still new to javascript, so I could be going about this the wrong way. If you can suggest a better way for me to do this, I would greatly appreciate it; always love to learn a new, better way!
Here goes:
I use the array iD to generate the URL's of the 10 JSON tables I'm pulling data from.
var set = $('.set');
var iD = [
"od6",
"o9mpc0w",
"ol96q2e",
"olqcdyz",
"ojfsm09",
"oijguh3",
"obmuic4",
"oup920g",
"ohwlz67",
"omk1ruv"
];
function photoSet(){
var idLength = iD.length;
for (var i = 0; i < idLength; i++) {
Once I iterate through the array, I use an ajax call to query each one.
$.ajax({
url:'https://spreadsheets.google.com/feeds/cells/1TBjwQNVRMNzuyZRxr9yX-fUKWw6gpUX69o9PQc79COs/' + iD[i] + '/public/values?alt=json',
async: true,
dataType: 'jsonp',
success: photos});
After they're queried, I retrieve the data from the cells of each JSON table and use it as an image src to insert into the document.
function photos(results){
for(var a = 1; a <= 1; a++) {
var imageEntry = results.feed.entry[a].content.$t;
set.append('<li class="image"><img src="'+ imageEntry +'"/></li>');
console.log(imageEntry);
}
}
}
}
photoSet();
Here's the issue: I need to have the resulting images in the same order as the array iD
Every time the page loads, the images are displayed in a different order. I know it's doing this because the queries are coming in at slightly different times, thus resulting in a different order every page load.
Does it have something to do with storing the ajax calls in variables, then grabbing them one at a time? Do I need another callback? As the nesting gets more complex, I get more confused :/
I've been searching high and low for how to do this, but just can't seem to figure it out. It seems with each new discovery I make, a new problem arises. Such is the nature of code, I suppose. Thank you all for your help :)
Also, I saw this question, but am having trouble figuring out how to apply its answer to my code. If I do, I'll remove this question promptly.