I am quite on lost on why Javascript does certain things in this way, or maybe I coded something wrong. But how come the code after $.getJSON gets executed before the callback is complete?
window.ratingCallback = function(data){
if(data[0].code == 3){
ratingHTML = buildHTML(0);
console.log('a'+ratingHTML);
return;
}
ratingHTML = buildHTML(data[0].rating);
console.log('b'+ratingHTML);
return;
};
function buildHTML(pageRating){
for(counter = 1; counter <= 5; counter++){
if(counter == pageRating){
ratingHTML += '<input name="star1" type="radio" class="star" value="'+counter+'" checked="checked"/>';
} else {
ratingHTML += '<input name="star1" type="radio" class="star" value="'+counter+'"/>';
}
}
return ratingHTML;
}
$.getJSON("https://domain.com/"+data+"?jsoncallback=?");
console.log(ratingHTML+"WHY DOES THIS EXECUTE FIRST AND NOT AFTER THE CALLBACK SINCE IT IS PLACED AFTER getJSON????");
The reason I want this to be different is that I need ratingHTML to be a global variable.
I understand that the callback gets called when the remote server has given a response, but can I let the rest of the script wait with further execution? Without placing all code in the callback function or any function?
Thank you very much!
Ice