【发布时间】:2012-06-04 17:56:52
【问题描述】:
像others 一样,我试图区分在移动浏览器和phonegap webuiview 中运行的javascript 代码。标准的解决方案是等待deviceready 事件触发,因为在它触发之后,您就知道您处于phonegap 中。但是你要等多久?
我有想要尽早运行的代码,因为我不希望我的用户坐在那里等待。但是我不想在 phonegap 初始化之前运行它,如果它要初始化的话。我正在寻找的是类似于devicenotready 事件,当cordova.js 之后的代码运行并确定没有任何东西可以附加时触发。或者我可以轮询一些变量来区分科尔多瓦仍在加载和科尔多瓦放弃尝试加载之间的区别。有区别吗?
我讨厌这个解决方案,但这是我想出的最好的解决方案。请告诉我有什么比这更好的:
function whenLoaded(callback,timeout) {
var when_loaded_needs_running = true;
document.addEventListener('deviceready', function() {
if( when_loaded_needs_running ) {
when_loaded_needs_running = false;
callback();
} else {
console.log("deviceready fired too late. whenLoaded already ran.");
}
});
window.setTimeout(function() {
if( when_loaded_needs_running ) {
when_loaded_needs_running = false;
console.log("deviceready didn't fire after "+timeout+"ms. running whenLoaded anyway.");
callback();
}
}, timeout);
}
【问题讨论】:
标签: javascript ios cordova