Thursday, May 14, 2015

Debug the first line of js code when page loads

Similar to debug cordova app when app starts, the similar approach can also be used to debug general javascript html page. 

Assume when page starts, it runs the below method, and you need to attach debugger on it.
       
var startFunc = function(){
           console.log("my start func");
}

startFunc();


Then adding the delayedStartForDebug method and also call  delayedStartForDebug(startFunc); will give you enough time to attach the debugger

var startFunc = function(){
           console.log("my start func");
}

var delayedStartForDebug = function(func) {
        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);
                            func.apply(originThis, arguments);
                        }
                    }
             }, 3000);
   }
   
   //startFunc();

   delayedStartForDebug(startFunc);

No comments:

Post a Comment