When you begin to invoke data asynchronously with a For loop, it is discovered that asynchronous execution occurs after the for loop. Of course, the results are not what I want to get.
In general this situation: we can solve the problem by the following method
1. JQuery $ajax
Set the Ajax parameter async to False, that is, with JS synchronization, the default is True (async).
for (int i = 0; i < 5; i++) {
var html= $.ajax ({url:url,async:false}). responsetext;
}
2, can be implemented by recursive method, when the asynchronous execution is done after the function of the call itself
$scope. goodslist = [];
function GetProducts (nameindex) {
for (Var i=0;i<name.length;i++) {//original for loop notation get data sort not fixed
var product = new AV. Query (' Product ');
Product Type
var Cid = new AV. Query (' classify ');
Cid.equalto ("GJZ", Name[nameindex]);
Cid.find (). Then (function (res) {
Product.equalto ("CID", parseint (res[0].id));
Product.equalto ("status", 1);
Product.descending ("Updatedat");
Product.limit (4);
var results = [];
Product Enquiry
Product.find (). Then (function (res2) {
Angular.foreach (Res2, function (result, index) {
var oo = Result.tojson ();
Oo.picurlarray = Angular.fromjson (Oo.picurlarray);
Oo.activename = Res[0].tojson (). title;
Oo.activenameen = Res[0].tojson (). GJZ;
for (var key in Oo.spec) {
$scope. $apply (function () {
Oo.product = Oo.spec[key];
Results.push (OO);
});
Break
}
});
$scope. Goodslist.push (results);
Console.log ($scope. goodslist);
/*
* Recursive implementation of asynchronous data before calling its own function
*/
nameindex++;
if (nameindex<name.length)
GetProducts (Nameindex);
Else
Return
});
});
// }
}
Indeed recommended, really hot, points merchandise, indeed good gift, sure enough special
var name = ["Grtj", "GRRM", "JFSP", "Grhl", "Grtjia"];
var nameindex = 0;
GetProducts (Nameindex);
JavaScript resolves asynchronous sequential execution issues