我怎样才能在jQuery的停止$。员额以异步方式运行? [英] How can I stop $.post in jQuery working asynchronously?

查看:105
本文介绍了我怎样才能在jQuery的停止$。员额以异步方式运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:

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.*/});

这篇关于我怎样才能在jQuery的停止$。员额以异步方式运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆