Tuesday, August 4, 2015

Delay javascript starting code for debug when loading html page

1. Include the below js file in the same folder of html file and also include the js file in html file's script element.
The file is also available at  https://github.com/jonathanli2/delaystart

//delaystart.js

var originalFunc;
var waitPeriod = 30;
function setDelayAt(obj, prop, waitSeconds){
originalFunc = obj[prop];
obj[prop] = delayedStartForDebug;
if (waitSeconds){
   waitPeriod = waitSeconds;
}
}

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

2. find a method that is used to start the application logic,  just before calling this starting method, add the below code
    setDelayAt(obj, "startingMethodName", 30);
The first parameter is the object that contains the start method, the second parameter is the start method name. The third parameter is optional for seconds to be delayed. 

No comments:

Post a Comment