jquery .when()触发完成回调,而延迟对象处于挂起状态

时间:2014-07-18 08:35:34

标签: jquery

我在DESKTOP浏览器(Chrome for Linux)上运行此代码:

var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
        deviceReadyDeferred.resolve();
}
$(document).one("mobileinit", function () {
        console.log('JQM is ready');
        jqmReadyDeferred.resolve();
});
$.when(deviceReadyDeferred, jqmReadyDeferred).then( doWhenBothFrameworksLoaded(), failure() );
function doWhenBothFrameworksLoaded() {
            alert('success :deviceReadyDeferred is '+deviceReadyDeferred.state());
            console.log('Phonegap and JQM are loaded');
            EVERYTHING();
}

并且alert给出了success :deviceReadyDeferred is pending ...而它甚至不应该开火,是吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您需要将函数引用作为参数传递给then()方法,不要在调用then()时调用回调方法(通过在函数名末尾添加()

$.when(deviceReadyDeferred, jqmReadyDeferred).then(doWhenBothFrameworksLoaded, failure);

演示:Fiddle