javascript 如果 deviceready 事件已经触发,如何检查cordova是否准备好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25341826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:24:02  来源:igfitidea点击:

How can I check if cordova is ready if the deviceready event has already fired?

javascripteventscordovaready

提问by Shawn

In the example app cordova provides through cordova create ..., the following code listens to the devicereadyevent:

在示例应用程序cordova提供 through cordova create ...,以下代码侦听deviceready事件:

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

This is nice, but what happens when the event is fired before I've had time to listen for it? As an example, replace the code from the example app (above) with the following:

这很好,但是当事件在我有时间侦听之前被触发时会发生什么?例如,将示例应用程序(上面)中的代码替换为以下内容:

bindEvents: function() {
    setTimeout(function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    }, 2000)
},

In this example, this.onDeviceReady is never called. Would there not be a better, more reliable way to check if cordova is ready? Something like this:

在此示例中,从未调用过 this.onDeviceReady。难道没有更好、更可靠的方法来检查科尔多瓦是否准备好了?像这样的东西:

bindEvents: function() {
    setTimeout(function () {
        if (window.cordovaIsReady) {
            this.onDeviceReady()
        } else {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        }
    }, 2000)
},

回答by frank

As per the cordova documentation

根据cordova文档

The deviceready event behaves somewhat differently from others. Any event handler registered after the deviceready event fires has its callback function called immediately.

deviceready 事件的行为与其他事件有些不同。在 deviceready 事件触发后注册的任何事件处理程序都会立即调用其回调函数。

As you can see if any event Handler is attached AFTERthe deviceready has fired it will be called immediately.
In a setTimeoutfunction thisis a no longer pointing to the intended object, the context is different. Therefore your handler will never be called.
You can try the below code by placing it in your <head>tag, where I am using global functions/variables (avoiding the thiscontext issues for sake of simplicity). This should show you an alert.

如您所见,在 deviceready 触发是否附加了任何事件处理程序,它将立即被调用。
setTimeout函数中,this不再指向预期对象,上下文不同。因此您的处理程序永远不会被调用。
您可以尝试将以下代码放在您的<head>标签中,我使用的是全局函数/变量(为了简单起见,避免了this上下文问题)。这应该会向您显示警报。

<script>
    function onDeviceReady () {
     alert("Calling onDeviceReady()");
    }

    setTimeout(function () {
            document.addEventListener('deviceready', onDeviceReady, false);
    }, 9000);
</script>

回答by jafarbtech

frankanswer really works. But the right way to handle this is not by adding timeout.

frank答案确实有效。但是处理这个问题的正确方法不是添加超时。

The devicereadyEvent Handler will be created while DOM is loading. So to use the event we should wait untill DOMContentLoaded. after that we can add listener to the devicereadyevent

deviceready事件处理程序将同时DOM加载创建。所以要使用事件我们应该等到DOMContentLoaded。之后我们可以向deviceready事件添加侦听器

document.addEventListener("DOMContentLoaded", function() {
    //alert("Calling DOMContentLoaded");
    document.addEventListener('deviceready', function(){
        //alert("Calling onDeviceReady()");
        callFirebase();
    }, false);
});