I just started to practice code in asynchronous programming. And I'm little bit confused, why I could not get the application working correctly.
I'm trying to load json data in a var jsonItems. Load the sound, and play.
function getJItems(jsonItems, callback){
$.getJSON( "data.json", function( data ) {
jsonItems.push(data);
});
callback(jsonItems);
}
var sI = [];
function loadSoundJson(jsonItems){
for (it =0 ; it < jsonItems[0].length ; it++){
sI[it] = new buzz.sound( "data/"+jsonItems[0][it].filesound, { formats: [ "ogg", "mp3"] });
}
};
This is how I try to call.
$( document ).ready(function() {
getJItems(jsonItems,loadSoundJson);
});
The error that I get is: jsonItems[0] is undefined
If I write, works fine.
$( document ).ready(function() {
getJItems(jsonItems);
window.alert("forced pause");
loadSoundJson(jsonItems);
});
So, I just want to be sure, that whole content (images, sound, data) will be loaded and ready before the application starts.
Maybe what I want is, know how to implement the correct way to making a good loading page. thanks in advance.