I'm lazy loading part of my code, some file containing classes that are then called in other parts, so I wrote a simple code that checks if a class instance exists and then execute it, or retry after 100ms.
let checkExist = setInterval(function() {
if (typeof(ClassInstanceName) === "object") {
ClassInstanceName.DoSomething();
clearInterval(checkExist);
}
}, 100);
This code works, but I need to generalize it in a self contained function
function WaitForClass(args) {
let instance = (args.instance === undefined ? "" : args.instance);
let callback = (args.callback === undefined ? "" : args.callback);
if (instance == "" || callback == "") {
console.log("Error -> " + instance + " " + callback);
}
else {
if (document[instance]) {
//it never find the instance
callback();
}
else {
setInterval(function() {
WaitForClass(args);
}, 100);
}
}
}
WaitForClass({instance:"ClassInstanceName", callback:function(){ ClassInstanceName.DoSomething();}});
The function is simple, but can't make it work. For compatibility with other code portions I need to check if the instance exist using a string containing the name, and not just the object representing it.