2013-06-06 122 views
1

我被困在一個地方。 我想要做的是我的2個功能,他們兩個都異步運行。所以我發現了jQuery的完成時間,並認爲使用它。jquery當不能正常工作

請參閱以下我的代碼,我使用: -

$.when(doOne(42, PersonId)) 
.done(function (strDisplayMessage) { 
    doTwo() 
}) 


function doOne(systemMessageId, personId) { 
    /* This function is used to make an AJAX call to the backend function to get all the customized system message. */ 
    $.ajax(
    { 
     url: "/Communications/GetSpecifiedSystemMessageAndCustomizeIt", 
     type: "POST", 
     data: { intSystemMessageId: systemMessageId, guidPersonId: personId }, 
     dataType: "text", 
     success: function (data) { 
return data; 
     }, 
     error: function (error) { 
      return "Error!"; 
     } 
    }); 
} 

function doTwo(){ 
...//do something 
} 

但他們還是異步運行。 任何人都可以幫助我嗎?

感謝名單

回答

0

您需要return$.ajax結果在doOne功能:

function doOne(...) { 
    return $.ajax({ 
     ... 
    }); 
} 

沒有明確return該函數返回undefined$.when()這將導致它立即觸發.done處理程序。

5

你需要從doOne

$.when(doOne(42, PersonId)) 
.done(function (strDisplayMessage) { 
    doTwo() 
}) 


function doOne(systemMessageId, personId) { 
    /* This function is used to make an AJAX call to the backend function to get all the customized system message. */ 
    return $.ajax(
     { 
      url: "/Communications/GetSpecifiedSystemMessageAndCustomizeIt", 
      type: "POST", 
      data: { intSystemMessageId: systemMessageId, guidPersonId: personId }, 
      dataType: "text", 
      success: function (data) { 
       return data; 
      }, 
      error: function (error) { 
       return "Error!"; 
      } 
     }); 
} 
返回Ajax對象