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.

2
  • Looks like you need a module loader. If only there was a way of asynchronously importing modules natively in modern browsers... Commented Jan 13, 2020 at 5: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. Commented Jan 17, 2020 at 3:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.