weixin_33717117 2014-06-24 16:01 采纳率: 0%
浏览 865

jQuery Ajax .done()行为

I am wondering about the behavior of the code below. Ajax.chan(tid) is the ajax call found below. The other three functions are not Ajax related. When I don't pass a parameter to Display.channelLoad, the code works as expected and these functions run after the ajax is complete. But if I change that to .done(Display.channelLoad()), this function runs before the ajax call is complete. This is probably some basic javascript knowledge that I am missing, but I would like to pass a parameter to Display.channelLoad and understand what is occurring here.

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'
        });
    }

Thank you.

  • 写回答

1条回答 默认 最新

  • weixin_33675507 2014-06-24 16:11
    关注

    From the jQuery docs on .done:

    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.

    What this means is that in your example the .done(List.articles) gets called with the result of $.ajax(). So it will get called with jqXHR object so it would look like this:

    List.articles(data, textStatus, jqXHR)
    

    And the same will happen for the other done calls in order.

    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上