0

機能している非同期AJAXリクエストを実行していwrappedます。$.ajax が Deferred オブジェクトであり、.promise を適切に使用できる場合 (チェック: Initially Loaded)、ajax がロードを完了する前に実行される 'Now really loaded' で同じことを行うことはできません。

function WSCall(method, data, callback, type, async, bg) {
    // .. code ..
    var promise = $.ajax({
        'url': useSampleData ? useSampleData || null,
        //'async': false,
        'type': 'POST',
        'dataType': (type == null) ? 'json' : type,
        'data': data,
        'beforeSend': bg ? null : LoadingBegin,
        'complete': bg ? null : LoadingEnd,
        'success': callback,
        'error' : bg ? null : function(jqXHR, textStatus, errorThrown) { networkError = 1; }
    });

    promise.done(function(){ console.log('Initially loaded') });
}

function aSyncEvent() { 
    WSCall(
        'status',
        {},
        function (data) {
            if (data.error) { 
                console.log('Error occured'); return ShowDialogAlert(data.error); }
            if (data.statusResult) {
                var parts = data.statusResult.split('-');
                if (parts[1] === '0') { 
                    sId = parts[0]; 
                    console.log('Wow its loaded!'); 
                    return true; 
                }               
            }
        }
    )   
}

$.when( aSyncEvent() ).then( function () { console.log('now really loaded')});

ajax が適切な順序で実行された後、最初にロードされ、そのロードが適切に表示されますが、ajax の実行が終了する前に「実際にロードされました」が表示されます。

この件に関して助けを求めます。

ありがとうマイク

4

1 に答える 1

0

遅延を返そうとしましたか?

function WSCall(method, data, callback, type, async, bg) {
    // .. code ..
    return promise.done(function(){ console.log('Initially loaded') });
}

function aSyncEvent() { 
    return WSCall(
        // .. code ..
    );
}
于 2012-08-14T11:35:39.223 に答える