2013-02-11 84 views
0

需要我的延期幫助,試圖使用.then和.done。但它不起作用。 它在我的servercall完成之前編寫我的console.log。如何讓我的工作延期

$.when(PersonAtlLawUpdate(personRef)).then(console.log('test')); 

function PersonAtlLawUpdate(personRef, cbFunc) { 
    var selectionPanel = $('div#SelectionPanel'), 
     fromdate = selectionPanel.find('input#FromDateTextBox')[0].defaultValue, 
     timeSpan = selectionPanel.find('select#TimeSpanDropdownList').data('timespanvalue'), 
     url = "MonthOverview.aspx/OnePersonAtlLawUpdate"; 
    $.ajax({ 
     url: url, 
     data: JSON.stringify({ personRef: personRef, fromdate: fromdate, timespan: timeSpan }), 
     type: "POST", 
     contentType: "application/json", 
     dataType: "JSON", 
     context: document.body, 
     success: function (atlError) { 
      changePersonAtlStatusIcon(atlError, personRef); 
      if (cbFunc != null) { 
       cbFunc(); 
      } 
      return atlError; 
     }, 
     error: function (xhr, status, errorThrown) { 
      //alert(errorThrown + '\n' + status + '\n' + xhr.statusText); 
     } 
    }); 
} 

回答

1

PersonAtlLawUpdate()應以與$.when()使用返回一個Deferred對象。

由於遞延你感興趣的是$.ajax()返回,你應該寫:

function PersonAtlLawUpdate(personRef, cbFunc) { 
    // [...] 
    return $.ajax({ 
     // ... 
    }); 
}; 
+0

它的工作。認爲來自服務器的每個數據都成爲延遲對象。添加你的返回和$ .when(PersonAtlLawUpdate(personRef))。然後(function(){使它工作(添加一個函數) – thatsIT 2013-02-11 12:13:32