0

我正在执行正在wrapped运行的异步 AJAX 请求。$.ajax 是 Deferred 对象,我可以正确使用 .promise(检查:Initially Loaded),那么我将无法对“现在真正加载”执行相同操作,这将在 ajax 完成加载之前执行。

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 回答