在cordova提供的示例应用程序中,通过
cordova create ...
命令创建的应用程序会监听deviceready
事件的以下代码:bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
这很好,但如果事件在我监听之前被触发会发生什么?举个例子,将上面示例应用程序的代码替换为以下内容:
bindEvents: function() {
setTimeout(function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
}, 2000)
},
在这个例子中,this.onDeviceReady从未被调用。难道没有更好、更可靠的方法来检查cordova是否已准备就绪吗?例如:
bindEvents: function() {
setTimeout(function () {
if (window.cordovaIsReady) {
this.onDeviceReady()
} else {
document.addEventListener('deviceready', this.onDeviceReady, false);
}
}, 2000)
},