可以调用复杂的函数来抑制$ .when吗?

时间:2011-10-28 21:27:25

标签: jquery

我有一个功能:

function someFunction(params) {
//Creates an object
//$.each(collection, funct() { /*do work on collection, essentially pushes items into an array */});

//JSON.stringify(results)

//$.ajax POST...this is what I want $.when to wait for

}

我设置了

$.when(someFunction(params)
).then(doNextThing);

我也试过

$.when(function() {someFunction(params) }
).then(doNextThing);
在回调之前调用

doNextThing$.eachstringify是否可能导致$.when失败?

1 个答案:

答案 0 :(得分:1)

someFunction需要返回$.when的对象才能正常工作。否则它相当于someFunction(params);$.when(undefined).then(doNextThing)。在此设置中,永远不会调用doNextThing

由于您正在等待ajax调用,它应该看起来像这样:

function someFunction(params) {
    ...
    return $.ajax(...);
}

$.when(someFunction(params)).then(doNextThing);