I’ve got a piece of code set up to check a condition, and based on that condition, either execute an ajax call to get a JSON object, or proceed to another form of processing. After the processing, I then do some stuff based on the data processed in the if/else statement.
I’m running into a problem, however. The code executes the if/else, and then continues on before the .get is finished processing, and thus my last bit of code doesn’t work properly. Is there a way to delay the processing of the rest of the code until the .get is completed?
The structure of my code is as follows:
if(filename != undefined){
$.get(filename, function(json){
$.each(json, function(data){
// Do stuff with the json
});
}, "json");
} else {
// Do some other processing
}
// Do some additional processing based on the results from the if/else statement
// This bit is getting processed before the .get has finished doing it's thing
// Therefore there isn't anything for it to act upon
Use the
async: falseoption for$.getto make synchronous requests. http://api.jquery.com/jQuery.ajax/NOTE: