jquery - After adding callback function, why is my variable modified inside it unaltered? -
i know asynchronicity, have followed steps of adding callback function why variable unaltered after modify inside of function? - asynchronous code reference but can access changes inside callback function. here code snippet. please help
function callback(point) { (i = 0; < point.length; i++) { mydate[i] = point[i].date_time; myvalue[i] = point[i].my_value; } (j = 0; j < mydate.length; j++) { var temp = new array(mydate[j], myvalue[j]); myseries[j] = temp; } alert("myseries in scope: " + myseries); //defined } alert("myseries out scope: " + myseries); // undefined getallmessagesforchart(callback); function getallmessagesforchart(callback) { var data = @html.raw(jsonconvert.serializeobject(this.model)) $.ajax({ url: '/messages/getmessagesforchat', data: { id: data.id, }, contenttype: 'application/html ; charset:utf-8', type: 'get', datatype: 'json' }).success(function (data) { callback(data); }).error(function (e) { alert("error " + e) }) }
i hope helps clear things you--
it not issue of scope. matter of order statements executed. if order amazon.com, go front stoop , expect find package? no, have wait drone there.
the myseries variable global variable. not "out of scope" outside callback() function. , it's not can't access changes outside callback() function; it's can't access changes before have been made. alert "myseries out scope" appears in code after callback() function defined, executed before callback() function executed.
even if change order of following 2 statements, alert still executed before callback function, because callback function not executed until ajax call returns, assynchronously.
getallmessagesforchart(callback); alert("myseries out scope: " + myseries); // undefined
Comments
Post a Comment