I am trying to load one of the js file using script src, but functions(methods) inside this src file will always take time to load. (Note: script tag's src= "emscripten_model.js" is generated with help of emscripten ).
I tried to use below function and callback , but callback function gets called even methods inside src are not loaded yet.
this.loadModel = function(path,onloadCallback) {
let script = document.createElement('script');
script.setAttribute('async', '');
script.setAttribute('type', 'text/javascript');
script.addEventListener('load', () => {
console.log("now ready to call callback!!!");
onloadCallback();
});
script.addEventListener('error', () => {
self.printError('Failed to load ' + path);
});
script.src = path;
let node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(script, node);
console.log("ccccccccccccc");
};
I tried settimeout or setinterval to avoid the above issue, But Is there any way I can handle without settimeout or setinterval .
Is there anyway I can handle better using callback or addeventlistener, please provide me piece of code to solve this.