这是我拥有的所有代码,我既没有得到xcode中的日志也没有得到deviceReady事件(我也没有得到任何其他平台。在Ubuntu + Android + Eclipse上我得到了控制台日志,但没有deviceReady。也没有chrome)
js / cordova-1.5.0.js存在并被加载,表示我已放入其中的警告声明。 我应该在哪里寻找线索? 在此先感谢;)
<div id="d"></div>
<script>
function foo() {console.log('test'); document.getElementById('d').innerHTML += 'called';}
window.setTimeout(foo, 5000);
window.setTimeout(foo, 15000);
window.setTimeout(foo, 25000);
window.setTimeout(foo, 35000);
alert('hi');
console.log('non timed console.log');
</script>
<script src="js/cordova-1.5.0.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert('deviceReady');
//somewhy this never happens
}
</script>
答案 0 :(得分:4)
Console.log仅适用于deviceReady事件
Phonegap为android和ios使用不同的phonegap.js文件,只有 android one随可下载档案一起发布。读 Dhaval的评论,以了解在哪里获得ios版本。
我使用Weinre进行调试,几乎错过了它覆盖了console.log方法, 因此console.log不适用于weinre
答案 1 :(得分:0)
正如Alex指出的那样,在PhoneGap设备准备好之后,console.log才可用。通过过早调用,您将触发参考错误。
删除所有现有的javascript,然后尝试此操作(使用您自己的自定义代码替换倒数第二行的提醒):
var app = {
// denotes whether we are within a mobile device (otherwise we're in a browser)
iAmPhoneGap: false,
// how long should we wait for PhoneGap to say the device is ready.
howPatientAreWe: 10000,
// id of the 'too_impatient' timeout
timeoutID: null,
// id of the 'impatience_remaining' interval reporting.
impatienceProgressIntervalID: null,
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// `load`, `deviceready`, `offline`, and `online`.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
// after 10 seconds, if we still think we're NOT phonegap, give up.
app.timeoutID = window.setTimeout(function(appReference) {
if (!app.iAmPhoneGap) // jeepers, this has taken too long.
// manually trigger (fudge) the receivedEvent() method.
appReference.receivedEvent('too_impatient');
}, howPatientAreWe, this);
// keep us updated on the console about how much longer to wait.
app.impatienceProgressIntervalID = window.setInterval(function areWeThereYet() {
if (typeof areWeThereYet.howLongLeft == "undefined") {
areWeThereYet.howLongLeft = app.howPatientAreWe; // create a static variable
}
areWeThereYet.howLongLeft -= 1000; // not so much longer to wait.
console.log("areWeThereYet: Will give PhoneGap another " + areWeThereYet.howLongLeft + "ms");
}, 1000);
},
// deviceready Event Handler
//
// The scope of `this` is the event. In order to call the `receivedEvent`
// function, we must explicity call `app.receivedEvent(...);`
onDeviceReady: function() {
app.iAmPhoneGap = true; // We have a device.
app.receivedEvent('deviceready');
// clear the 'too_impatient' timeout .
window.clearTimeout(app.timeoutID);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
// clear the "areWeThereYet" reporting.
window.clearInterval(app.impatienceProgressIntervalID);
console.log('Received Event: ' + id);
myCustomJS(app.iAmPhoneGap); // run my application.
}
};
app.initialize();
function myCustomJS(trueIfIAmPhoneGap) {
// put your custom javascript here.
alert("I am "+ (trueIfIAmPhoneGap?"PhoneGap":"a Browser"));
}
答案 2 :(得分:0)
我知道这个问题是在9个月前被问到的,但我偶然发现了同样的问题。
如果您希望调试消息显示在weinre
控制台中,则必须调用:
window.cordova.logger.useConsole(false);
在deviceready
之后。
<强>更新强>
您似乎需要运气才能将控制台消息输入weinre
- 这很糟糕:(