I understand how $.get()
fetches asynchronously. But how can I force a synchronous call?
I require preferences loaded from a json file to be used throughout many javascript calls throughout an app. How can I load these preferences not into the DOM, but rather a variable to be used throughout the rest of the app?
Here's how it works asynchronously:
$.get('my.json',function(data) { var myJson = data; });
console.log(myJson); // undefined
$('.myElement').html(myJson.title); // this will asynchronously load the contents of myJson.title into `.myElement`
I've read that I should try:
$.get('my.json',WrapperFunction(data));
WrapperFunction(data) {
// do all of your javascript calls here
}
console.log(myJson); // undefined
Any other ideas to not move on until get completes?