Setting a variable to JSON dataset without having to put logic that needs to follow the retrieval of the JSON object inside the JSON callback.
var json = (function () {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': my_url,
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})();
var myData = getSomething();
function getSomething(){
var result = null;
$.ajax({
async: false,
url: "url",
dataType: "json",
success: function (data) {
result = data;
}
});
return result;
}
function getSomething () {
$.getJSON("url", data, myCallback)
}
function myCallback(data){
// Do some processing!
}
function get_something(){
$.getJSON("get_something.php",function(json){
processMyJson(json);
});
}
function processMyJson(json){
console.log(json);
}