0

This code leads to :Uncaught error : check is not defined.Can some one help regarding this code snippet. Please find below code

The check() is define in external .js file

Method in script tag ...
    <script>

    loadScriptfinal("/js/VendorPaymentInfo/"+coreId+".js", function() {
                             check();
                            });


    function loadJS(file,callback) {
        var jsElm = document.createElement("script");
        jsElm.type = "application/javascript";
        jsElm.src = file;

        jsElm.onload = function() {
            callback(); 
        }

        // finally insert the element to the body element in order to load the script
        document.body.appendChild(jsElm);
    }

    </script>
Deepak Dhaka
  • 9
  • 1
  • 4
  • `The check() is define in external .js file` – Umair Mohammad Jun 07 '18 at 04:13
  • You're calling loadJS like here https://stackoverflow.com/questions/5235321/how-do-i-load-a-javascript-file-dynamically right ? – Umair Mohammad Jun 07 '18 at 04:17
  • My concern here is not about loading js, it's getting loaded, but this whole process happen in onload where check method executed before loading js. I wanted to restrict that using callback but seems callback not working – Deepak Dhaka Jun 07 '18 at 04:26

1 Answers1

0

You can watch till the external file is loaded and then call loadScriptfinal function.

var  timer = null;
var timer = setInterval(()=>{
if (window.check && window.check.constructor === Function) {
    clearInterval(timer);
    loadScriptfinal("/js/VendorPaymentInfo/"+coreId+".js", function() {
        check();
     });
     }
},100)