最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I listen for deviceready multiple time on Cordova 2.5? - Stack Overflow

programmeradmin1浏览0评论

Could I register 'deviceready' event for each html page? I use the initialize codes of Cordova 2.5 and it work fine. When I copy these codes to a new html file, It always call to the initialize() function of the index.html.

var app = {
    // 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);
    },
    // 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.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        console.log('Received Event: XXX ' + id);

    }
};

=============== HTML file:

....

 <script type="text/javascript">
                app.initialize();
                </script>

....

Could I register 'deviceready' event for each html page? I use the initialize codes of Cordova 2.5 and it work fine. When I copy these codes to a new html file, It always call to the initialize() function of the index.html.

var app = {
    // 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);
    },
    // 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.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        console.log('Received Event: XXX ' + id);

    }
};

=============== HTML file:

....

 <script type="text/javascript">
                app.initialize();
                </script>

....

Share Improve this question edited Mar 26, 2013 at 7:45 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Mar 26, 2013 at 4:14 Nguyen Minh BinhNguyen Minh Binh 24.5k31 gold badges120 silver badges173 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Device ready will fire only once. It will fire next time only when the app is killed and opened again.

发布评论

评论列表(0)

  1. 暂无评论