javascript - How to use array values in asynchronous function -
i have array, values want use input asynchronous function. when iterate through array , send them asynchronous function, calls asynchronous function seems done last value in array.
i thought kind of problems solved callbacks, function library i'm using, doesn't provide callback.. should make asynchronous function treat individual array values??
var libraryname = new library("#div"); $.post("myfile.php", {somedata}, function(data){ console.log(data); //prints correct values for(i in data){ libraryname.asynchronousfunction({ name: data[i].name // <--this value becomes last value in data-array calls }); } });
try because of closures:
$.post("myfile.php", {somedata}, function(data){ console.log(data); //prints correct values for(i in data){ (function(elem) { libraryname.asynchronousfunction( {name: elem.name} ); })(data[i]); } });
Comments
Post a Comment