My code:
<script type="text/javascript">
console.log('First Script');
var html = JSON.parse( document.getElementById('json').innerHTML);
var associatedFiles = html['associatedFiles'];
if (typeof(associatedFiles) == 'undefined') {
console.log('No files to compile');
} else {
console.log('associatedFiles Found');
for (i in associatedFiles){
$.get(associatedFiles[i], async=false).then(function(data, status){
$.merge(html.data, data.data);
console.log('html.data.length:',html.data.length);
});
}
$("#json").html(JSON.stringify(html));
console.log('associatedFiles Compiled');
console.log($("#json").html());
}
</script>
The console shows:
First Script
associatedFiles Found
associatedFiles Compiled
<<$('#json').html() contents>>
html.data.length:903
html.data.length:1354
html.data.length:180
Why are the lines of code after the for loop being executed before the for loop?? I'm fairly new to javascript and jquery so really hoping someone can explain what's going on here. ...