Friday, December 19, 2014

How to debug cordova onDeviceReady event

After loading the html page, the cordova will immediately call onDeviceReady event handle, so it is hard to set break pointer to debug the logic in onDeviceReady event.
One option is use javascript setInterval method, so it will hold the javascript until you change the bWait flag from javascript console window

The sample code looks like


  var onDeviceReady = function() {
        var originThis = this;
        var bWait = true;
        var bDone =false;
        var i = 0
        var timer = setInterval(
                function(){
                    if (bWait && i < 10 ){
                        i++;
                        console.log("set bWait to false from console window to continue");
                    }
                    else{
                        if (!bDone){
                            bDone = true;
                            clearInterval(timer);
                            yourOriginalOnDeviceReady.apply(originThis, arguments);
                        }
                    }
                }, 3000);
    }

Actually, this can also be used in other cases for attaching javascript debugger, particularly for the js script loaded into the DOM at runtime by xhr request. Just wrap the original function with the above function should do it.

1 comment:

  1. I want to always read your blogs. I love them Are you also searching for Nursing case study writing services? we are the best solution for you. We are best known for delivering Nursing case study writing services to students without having to break the bank

    ReplyDelete