我怎样才能在jQuery的停止$。员额以异步方式运行?员额、方式、jQuery

2023-09-10 14:20:56 作者:心境

我有以下要求:

getData(0);
getData(1);
getData(2);

这些调用javascript函数:

These call a javascript function:

function getData(Code) {
  $.post('/adminA/GetData',
                { Code: Code },
  function (data) {

现在的问题是,我的数据获取返回坏了。有没有办法,我可以在订单数据返回的方式?

The problem is that my data gets returned out of order. Is there a way that I can have the data return in order?

推荐答案

调用这三个Ajax请求同步将冻结您的浏览器。你会更好使用jQuery的递延对象。试试这个:

Calling these three ajax requests synchronously will freeze your browser. You'd be better off using jQuery's Deferred objects. Try this:

function getData(Code) {
    return $.post('/adminA/GetData', { Code: Code }, function (data) {/*etc.*/});
}

getData(0).done(function() {
    getData(1).done(function() {
        getData(2);
   });
});

附录

您应该也考虑您的通话合并成一个单一的一个,更改服务器的逻辑来处理它。这最终将快于三点要求:

Addendum

You should also consider combining your calls into a single one, and change the server logic to handle it. This will ultimately be faster than three requests:

function getData(firstCode, secondCode, thirdCode) {
    $.post('/adminA/GetData', {
        codeOne   : firstCode,
        codeTwo   : secondCode,
        codeThree : thirdCode
    }, function (data) {/*etc.*/});