Within my function, I have defined a global variable called window.playerLibrary
. Interestingly, when I check the value of window.playerLibrary
within the function itself (`var check #1`), it returns a value. However, if I try to check it just outside of the ajax call or after calling the function, the variable is undefined:
function generateAllCards() {
$.ajax({
type: "POST",
url: "processGame",
data: {
mode: "generateCards"
},
dataType: "JSON",
success: function(data) {
window.playerLibrary = data.playerLibrary;
// var check #1
console.log(window.playerLibrary);
}
});
// var check #2
console.log(window.playerLibrary);
}
generateAllCards();
// var check #3
console.log(window.playerLibrary);
Reflecting on this issue, I suspect that the variable definition within the ajax call is not being captured during checks #2 and #3 due to sequencing. Is there a solution to address this problem?