1

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.

Siva K V
  • 10,561
  • 2
  • 16
  • 29
Vasant
  • 101
  • 2
  • 10
  • Looks like you need a module loader. If only there was a way of [asynchronously importing modules natively](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) in modern browsers... – BlueWater86 Jan 13 '20 at 05:00
  • @BlueWater86 , thanks for guidance. In My case I have a emscrippten js codes enclosed in a function called: function module_calculation{ /*js code is here*/} and exported this as : var Module_CalAlign = module_calculation();. I written in plain jaavscript function , Is there anyway I can handle to wait until this module_calculation() function loads. – Vasant Jan 17 '20 at 03:05

1 Answers1

1

this is not an answer but i believe this will help you.

if jquery its gong to be $(document).ready(function(){}); if pure javascript refer to link below:

click here

Nyuu
  • 121
  • 1
  • 1
  • 6
  • This not the right one. In My case I have a emscrippten js codes enclosed in a function called: function module_calculation{ /*js code is here*/} and exported this as : var Module_CalAlign = module_calculation();. I written in plain jaavscript function , Is there anyway I can handle to wait until this module_calculation() function loads. – Vasant Jan 17 '20 at 03:06
  • can try this https://stackoverflow.com/questions/10808109/script-tag-async-defer see if it can help you – Nyuu Jan 17 '20 at 04:47