That title was a mouthful. Here’s what I’m trying to do in code below. The callback in this case is in the jQuery $.get function.
function getMapMarkup(loadUrl, myVar) {
me = myVar;
$.get(
loadUrl,
{ var1: "hello", var2: "world" },
function(responseText) {
me = responseText;
myVar = me; //doesn't work.
},
"html"
);
}
Is there a way to change the value of myVar in the function(responseText) callback, so I can use it in my program later on? Or is there another (better) way to go about what I’m trying to do?
If by
later onyou mean immediately after the$.getcall then no, there is no way because AJAX is asynchronous and the$.getreturns immediately and the success callback can be executed much later. The only reliable way to know when this happens is to put the code that depends on its result inside thesuccesscallback. You could also call some other function inside the success callback passing it the result of the AJAX call.