Javascript variable not updating value -


this question has answer here:

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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -