我想知道下面代码的行为。 Ajax.chan(tid)
是下面的 ajax 调用。其他三个函数与 Ajax 无关。当我不向 Display.channelLoad 传递参数时,代码将按预期工作,并且这些函数将在 ajax 完成后运行。但如果我将其更改为 .done(Display.channelLoad())
,则该函数会在 ajax 调用完成之前运行。这可能是我缺少的一些基本 javascript 知识,但我想将参数传递给 Display.channelLoad
并了解这里发生的情况。
AjaxCall.chan(tid).done(List.articles)
.done(Display.channelLoad)
.done(Display.unblockUI);
AjaxCall.chan()
var AjaxCall = {
chan: function(tid) {
return getArticles = $.ajax({
url: "http://ainonline.com/api/channel/" + tid,
context: document.body,
dataType: 'jsonp'
});
}
谢谢。
最佳答案
来自 .done
上的 jQuery 文档:
The deferred.done() method accepts one or more arguments, all of which can be either a single function or an array of functions. When the Deferred is resolved, the doneCallbacks are called. Callbacks are executed in the order they were added. Since deferred.done() returns the deferred object, other methods of the deferred object can be chained to this one, including additional .done() methods. When the Deferred is resolved, doneCallbacks are executed using the arguments provided to the resolve or resolveWith method call in the order they were added. For more information, see the documentation for Deferred object.
这意味着在您的示例中,将使用 $.ajax()
的结果调用 .done(List.articles)
。所以它将被调用 jqXHR
对象,所以它看起来像这样:
List.articles(data, textStatus, jqXHR)
对于其他按顺序完成的调用也会发生同样的情况。
关于javascript - Jquery Ajax .done() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391306/