最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery $.when() not working as expected - Stack Overflow

programmeradmin0浏览0评论

I have three functions. One is an AJAX call, one is a callback function given to this AJAX function, and the last one is a pletely independent function that waits for the AJAX functions to finish. The third function has to remain pletely independent from the AJAX function, it can not be passed as a parameter to the AJAX function. My code:

doAjaxStuff: function(callbackFunction){
     $.ajax(
          // do AJAX call
          // With the AJAX data, create HTML div elements 
          callbackFunction();
     )
}

callMeMaybe: function(data){
     //do stuff with the return data from the AJAX call
}

patientFunction: function(){
    $.when(this.doAjaxStuff).done(function(){
         alert("doAjaxStuff() has finished!");
         // do stuff to the created HTML div elements.
    });
}

But I never get the alert message to show up. The AJAX call and callback function are performed succesfully, but the $.when() function is never triggered. What is going wrong in this code?

I have three functions. One is an AJAX call, one is a callback function given to this AJAX function, and the last one is a pletely independent function that waits for the AJAX functions to finish. The third function has to remain pletely independent from the AJAX function, it can not be passed as a parameter to the AJAX function. My code:

doAjaxStuff: function(callbackFunction){
     $.ajax(
          // do AJAX call
          // With the AJAX data, create HTML div elements 
          callbackFunction();
     )
}

callMeMaybe: function(data){
     //do stuff with the return data from the AJAX call
}

patientFunction: function(){
    $.when(this.doAjaxStuff).done(function(){
         alert("doAjaxStuff() has finished!");
         // do stuff to the created HTML div elements.
    });
}

But I never get the alert message to show up. The AJAX call and callback function are performed succesfully, but the $.when() function is never triggered. What is going wrong in this code?

Share Improve this question edited Oct 7, 2014 at 9:33 yesman asked Oct 7, 2014 at 9:07 yesmanyesman 7,84816 gold badges63 silver badges127 bronze badges 2
  • Q: Do you mean to call callMeMaybe with the data from the ajax call? – iCollect.it Ltd Commented Oct 7, 2014 at 9:34
  • I remend $.ajax().always() for 3rd function. $.ajax() .done() .fail() .always() always will trigger when the ajax finish everything. – Kai Commented Oct 7, 2014 at 10:59
Add a ment  | 

1 Answer 1

Reset to default 8

To use when you need to return the ajax deferred/promise from your doAjaxStuff method:

doAjaxStuff: function(callbackFunction){
     return $.ajax(
          // do AJAX call
          callbackFunction();
     )
}

As you mention, the callback needs to be supplied with the current syntax, but with promises you no longer need callbacks.

doAjaxStuff: function(){
     return $.ajax(
          // do AJAX call
     );
}

Call with:

patientFunction: function(){
    var self = this;   // Need to retain a reference to this
    $.when(this.doAjaxStuff()).done(function(data){
         alert("doAjaxStuff() has finished!");
         // do stuff to the created HTML div elements.
         self.callMeMaybe(data);
    });
}
发布评论

评论列表(0)

  1. 暂无评论