Inside a $.getJSON success function, I first trigger another element's click event:
$('#' + data[0].ID).trigger('click');
The triggered click event has its own $.getJSON method to load a bunch of data into divs. The next line after the triggered event:
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data!
}
At first the $.each didn't appear to be doing anything, but I added an alert right after the triggered event. After responding to the alert, the code in $.each shows what it's supposed to.
I'm guessing $.each is running before the click event finishes loading the data.
setTimeout pauses long enough for the click event to load data, but I'd rather not set an arbitrary time:
setTimeout(function() {
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data!
}
}, 1000);
I also tried $.when and $.then to no avail (although adding alert before $.each inside $.then creates delay for $.each to work):
$.when($('#' + data[0].ID).trigger('click')).then(function () {
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data!
})