2

このようなAjax関数を取得しました

function PersonAtlLawUpdate(personRef) {
var selectionPanel = $('div#SelectionPanel');
var fromdate = selectionPanel.find('input#FromDateTextBox')[0].defaultValue;
var timeSpan = selectionPanel.find('select#TimeSpanDropdownList').data('timespanvalue');
var 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);
    },
    error: function (xhr, status, errorThrown) {
        //alert(errorThrown + '\n' + status + '\n' + xhr.statusText);
    }
});

}

1 つの関数で、次のように 2 回実行する必要があります。

    PersonAtlLawUpdate($(gMarkedCell).parent("tr").attr("personref"));
    PersonAtlLawUpdate(pRef);

起こりうる問題は、場合によっては 100% 機能しないことです。関数の 1 つで dom は更新されません。そして、もう一方がそれを「上書き」しているためだと思います。

では、最初の "PersonAtlLawUpdate" が完了した後に 2 番目の "PersonAtlLawUpdate" が実行されるようにするにはどうすればよいでしょうか? 遅らせるのは良くないようです。ajax呼び出しでasyncをfalseに設定するのは良い解決策ですか?

編集して、このように試して、私の成功にconsole.logを配置しました。ただし、「すべて完了」は最初に実行されます。

$.when(PersonAtlLawUpdate($(gMarkedCell).parent("tr").attr("personref")), PersonAtlLawUpdate(pRef)).then(function (){console.log("all complete")});
4

3 に答える 3

1

最初の関数が実行された直後に実行されるように、コールバック関数を使用できます。

PersonAtlLawUpdate($(gMarkedCell).parent("tr").attr("personref"), function(){
  PersonAtlLawUpdate(pRef);
});

または、問題を再考して、同じ関数を 2 回呼び出す必要のない解決策を考え出すこともできます。たぶん、これを行う必要はありません。

于 2012-12-13T13:31:23.057 に答える
0

@Kyokasuigetsuが示唆しているのは、PersonAtlLawUpdateメソッドを変更して、オプションの2番目のパラメーター、つまり成功コールバックで呼び出す必要があるコールバック関数を受け入れる必要があることだと思います。

function PersonAtlLawUpdate(personRef, cbFunc) {
var selectionPanel = $('div#SelectionPanel');
var fromdate = selectionPanel.find('input#FromDateTextBox')[0].defaultValue;
var timeSpan = selectionPanel.find('select#TimeSpanDropdownList').data('timespanvalue');
var 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();
    },
    error: function (xhr, status, errorThrown) {
        //alert(errorThrown + '\n' + status + '\n' + xhr.statusText);
    }
});

そして、次のように呼び出します。

PersonAtlLawUpdate($(gMarkedCell).parent("tr").attr("personref"), function(){
  PersonAtlLawUpdate(pRef);
});
于 2012-12-13T15:39:26.757 に答える
0

関数から呼び出す場合、例はreturn正常に機能します。$.ajaxPersonAtLawUpdate

$.whenajax 呼び出しへの参照が必要なのでDeferred、関数から (ajax 呼び出し)を返すようにしてください。

function PersonAtlLawUpdate(personRef) {
    var selectionPanel = $('div#SelectionPanel');
    var fromdate = selectionPanel.find('input#FromDateTextBox')[0].defaultValue;
    var timeSpan = selectionPanel.find('select#TimeSpanDropdownList').data('timespanvalue');
    var url = "MonthOverview.aspx/OnePersonAtlLawUpdate";
    //SEE THE NEXT LINE
    return $.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);
        },
        error: function (xhr, status, errorThrown) {
            //alert(errorThrown + '\n' + status + '\n' + xhr.statusText);
        }
    });
}

使用:

$.when(PersonAtLawUpdate(ref1), PersonAtLawUpdate(ref2)).done(function(xhrRef1, xhrRef2) {
    //do stuff w/ results from both calls
    //if you return something from the server, 
    //the results will be available in xhrRef1[0] 
    //and xhrRef2[0], respectively (order they 
    //appear in the when(), not in the order they execute
});
于 2012-12-13T15:39:40.350 に答える