Is the callback on jQuery's getScript() unreliable or am I doing something wrong?

后端 未结 11 1286
Happy的楠姐
Happy的楠姐 2020-12-03 03:17

I\'m using the following bit of script to load another one:

$.getScript(\"CAGScript.js\", function () {
    try {
        CAGinit();
    } catch(err) {
              


        
11条回答
  •  情话喂你
    2020-12-03 03:42

    In order to make sure CAGScript.js is LOADED and EXECUTED before calling CAGinit function, the surest way is to have the function call inside CAGScript.js.

    CAGScript.js:

    
    
        ...
        /*
        your code
        */
        function CAGinit(){
        ...
        }
        ...
        /* last line */
        CAGinit();
    
    

    and then, in your main file just call getScript():

     
    
        $.getScript("CAGScript.js");
    
    

提交回复
热议问题