链ajax并按顺序执行.Jquery延期

Joe*_*ito 15 javascript ajax each jquery deferred

我有3个需要ajax才能完成的进程.但它是异步的,它无法做我想做的事情..

让我们说:

function a(param1, param2) {
     $.post(..., function(result){
         if(result){
            b();
         } else {
            console.log("failed a");
         }
     })
}

function b() {
      $.post(..., function(result){
         if(result){
            c();
         } else {
            console.log("failed b");
         }
     })
}

function c() {
     $.post(..., function(result){
         if(result){
            console.log("successful");
         } else {
            console.log("failed b");
         }
     })
}
Run Code Online (Sandbox Code Playgroud)

我希望它像这样执行

a
b
c
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,该代码将完美地运行..但是如果使用循环.

 var data = [{param1 : 1235, param2: 3214},  {param1 : 5432, param2: 9876}];

 $.each(data, function(k,v){
      a(v.param1, v.param2)
 });
Run Code Online (Sandbox Code Playgroud)

它不会按预期工作,只会这样做:

a
a
b
b
c
c
Run Code Online (Sandbox Code Playgroud)

代替

a
b
c
a
b
c
Run Code Online (Sandbox Code Playgroud)

Bee*_*oot 15

写这种东西的方法有很多种.

灵活的方法是与"序列"分开的"行动",允许:

  • 函数a,b,c用于启动异步(ajax)动作,而不知道它们如何被排序
  • a,b,c可重复使用,作为一个或多个序列的一部分或根据需要单独使用.

这是一种编码这种方法的方法,.then()专门用于链接逻辑:

function a() {
    return $.post(...).then(function(result) {
        if(result)
            return result;//continue on "success" path.
        else
            return $.Deferred().reject('a').promise();//convert success to failure.
    }, function() {
        return 'a';//continue on failure path.
    });
}
function b() {
    return $.post(...).then(function(result) {
        if(result)
            return result;//continue on "success" path.
        else
            return $.Deferred().reject('b').promise();//convert success to failure.
    }, function() {
        return 'b';//continue on failure path.
    });
}
function c() {
    return $.post(...).then(function(result) {
        if(result)
            return result;//continue on "success" path.
        else
            return $.Deferred().reject('c').promise();//convert success to failure.
    }, function() {
        return 'c';//continue on failure path.
    });
}

a().then(b).then(c).then(function() {
    console.log("successful");
}, function(id) {
    console.log("failed: " + id);
});
Run Code Online (Sandbox Code Playgroud)

或者,如果你想拥有一个异步函数,a从循环中调用,那么代码可能是这样的:

function a(obj) {
    return $.post(...).then(function(result) {
        if(result)
            return result;//continue on "success" path.
        else
            return $.Deferred().reject(obj.id).promise();//convert success to failure.
    }, function() {
        return obj.id;//continue on failure path.
    });
}

var data = [{id:'A', param1:1235, param2:3214},  {id:'B', param1:5432, param2:9876}];
//Note how IDs are included so these data objects can be identified later in failure cases.

var dfrd = $.Deferred();//starter Deferred for later resolution.
var p = dfrd.promise();//A promise derived from the starter Deferred, forming the basis of a .then() chain.

//Build a .then() chain by assignment
$.each(data, function(i, obj) {
    p = p.then( function() {
        return a(obj);
    });//By not including a fail handler here, failures will pass straight through to be handled by the terminal .then()'s fail handler.
});

//Chain a terminal .then(), with success and fail handlers.
p.then(function() {
    console.log("successful");
}, function(id) {
    console.log("failed: " + id);
});

dfrd.resolve();//Resolve the starter Deferred to get things started.
Run Code Online (Sandbox Code Playgroud)


the*_*abb 6

你可以使用jQuery的延迟对象和使用'then'链接异步调用,如ajax调用.

你也可以改变它来使用返回延迟的promise对象的函数,而不是像我在我的例子中那样的ajax调用.

http://jsfiddle.net/q4cFv/

(异步函数示例:http://jsfiddle.net/q4cFv/1/)

$(function() {
    var delay = 3,
        span = $('span'),
        posts = [
            {
                input1: 'My name 1',
                input2: 'My address 1',
                input3: 'My country 1'
            },
            {
                input1: 'My name 2',
                input2: 'My address 2',
                input3: 'My country 2'
            },
            {
                input1: 'My name 3',
                input2: 'My address 3',
                input3: 'My country 3'
            },
            {
                input1: 'My name 4',
                input2: 'My address 4',
                input3: 'My country 4'
            }
        ],
        looper = $.Deferred().resolve();

    $.each(posts, function(i, data) {
        looper = looper.then(function() {
            return $.ajax({
                data: {
                    json: JSON.stringify(data),
                    delay: delay
                },
                method: 'post',
                url: '/echo/json/',
                dataType: 'json'
            }).done(function(response) {
                span.append('Response:<br />');
                for(key in response) {
                    span.append(key + ': ' + response[key] + '<br />');
                }
                $('span').append('Waiting ' + delay + ' seconds<br /><br />');
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)


Ben*_*aum 3

您的问题是您a一次调用所有 s 但您想等待第一个周期然后再进入下一个周期。您需要等待上一个“a”周期完成后再开始下一个周期。

假设 a、b、c 接受回调并将其传递,

a 看起来像

function a(param1, param2,callback) {
     $.post(..., function(result){
         if(result){
            b(callback);
         } else {
            console.log("failed a");
         }
     })
}
Run Code Online (Sandbox Code Playgroud)

b 会像这样:

function b(callback) {
      $.post(..., function(result){
         if(result){
            c(callback);
         } else {
            console.log("failed b");
         }
     })
}
Run Code Online (Sandbox Code Playgroud)

c 看起来像:

function c(callback) {
     $.post(..., function(result){
         if(result){
            console.log("successful");
         } else {
            console.log("failed b");
         }
         callback();
     })
}
Run Code Online (Sandbox Code Playgroud)

这让我们知道一个周期何时完成。这让我们可以写:

var data = [{param1 : 1235, param2: 3214},  {param1 : 5432, param2: 9876}];

var index = 0;
(function updateData(){
    a(data[index].param1,data[index].param2,function(){ //call a with the data
        index++;//update the index 
        updateData(); // start the next cycle
    });
});
Run Code Online (Sandbox Code Playgroud)