JavaScript resolves asynchronous sequential execution issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.