i have created this class to check if the function has been loaded. It works fine, except that I want to avoid using eval to evaluate the function name from a variable. I appreciate any help with this. Thanks.
class scriptLoader {
constructor(fn, max_try) {
this.fn = fn; //the func name being passed as a string
this.count = 0; //init count start value
this.max_try = max_try; //the max times to try
}
waitForScriptToLoad() {
'use strict';
let my_function = eval(this.fn); //this evaluate the string, i.e 'myfunc' to myfunc()
if(typeof my_function === "function") {
my_function();
} else {
if(this.count<this.max_try) {
var that = this;
setTimeout(function() {that.count++; that.waitForScriptToLoad();},100); //wait 100ms and try again
} else {
return false;
}
}
}
}
I instantiate the class like so:
loadData = new scriptLoader('Worker.fetch_form_data', 3);
loadData.waitForScriptToLoad();