Javascript variable not updating value -
this question has answer here:
- how return response asynchronous call? 21 answers
this makinkg me crazy, cant outside variable inside data
sorry bad formating i'm writing phone, if can helpme out format appreciate it
window.getreasons = function(line) { var $reasons; $reasons = ""; $.get(window.location.protocol + "//" + window.location.host + "/" + "alllinereasons.js?line=" + line, function(returndata) { $reasons = returndata.tostring(); return console.log("t_t ----->" + returndata.tostring()); }, "html"); console.log($reasons); return $reasons; };
the important thing understand $.get()
asynchronous default. happening console.log()
, return
statements follow call get()
executing before get()
has returned it's value.
you might utilize when()
method here handle deferred object returned get()
window.getreasons = function(line) { var reasons = ''; $.when( $.get(window.location.protocol + "//" + window.location.host + "/" + "alllinereasons.js?line=" + line) ).done(function(jqxhr) { data = jqxhr[0]; reasons = data.tostring(); }); console.log(reasons); return reasons; }
Comments
Post a Comment