Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getScript创建引用错误_Javascript_Jquery_Referenceerror_Getscript - Fatal编程技术网

Javascript getScript创建引用错误

Javascript getScript创建引用错误,javascript,jquery,referenceerror,getscript,Javascript,Jquery,Referenceerror,Getscript,我有一些js脚本,在开始其余代码之前,我会将它们加载到我的main.js。然而,在测试过程中,我注意到,有时它会产生以下引用错误(8个页面负载中有1个或其他) ReferenceError:未定义createContainer 现在,我能想到的得到这个错误的唯一原因是,当我执行startScript()函数时,并不是所有的文件都已加载或完全可访问 现在,也许我把其他的.js文件包含到我的main.js中是完全错误的,所以我想听听你对此的想法 main.js如下所示: $(document).r

我有一些js脚本,在开始其余代码之前,我会将它们加载到我的
main.js
。然而,在测试过程中,我注意到,有时它会产生以下引用错误(8个页面负载中有1个或其他)

ReferenceError:未定义createContainer
现在,我能想到的得到这个错误的唯一原因是,当我执行
startScript()
函数时,并不是所有的文件都已加载或完全可访问

现在,也许我把其他的.js文件包含到我的
main.js
中是完全错误的,所以我想听听你对此的想法

main.js如下所示:

$(document).ready(function() {

    //sets and array of files that should be loaded before anything every happens
    var arrFilesToLoad = [  'scripts/global/variables.js',
                            'scripts/global/objects.js',
                            'scripts/global/classes.js'];
    var _error;

    //walks through the array of items that should be loaded and checks for fails
    $.each(arrFilesToLoad , function (key) {
        $.getScript(arrFilesToLoad[key])
            //when a file is loaded with succes
            .done(function () {
                //on default send a message to the console
                console.log(arrFilesToLoad[key] + 'loaded succesfully');
                //if every item is loaded start the script
                if(key == (arrFilesToLoad.length - 1)){
                    startScript();
                }
            })
            //when a file fails to load
            .fail(function () {
                //add the file that failed to load to a string message
                _error += arrFilesToLoad[key] + " - failed to load. \n";
                //show an alert with what file failed to load
                if(key == (arrFilesToLoad.length - 1)){
                    alert(_error);
                }
            });
    });

    function startScript () {
        //set a variable which contains a function that returns a DIV with styling
        var oContainer = createContainer();
        var oMainMenu = new Menu(arrMainMenu);
        $(oContainer).append(createMenu(oMainMenu));
        $('body').append(oContainer);
    }

});

问题是,您正在加载3个脚本,其中可能只有一个包含
createContainer()
函数,但您在加载最后一个请求时执行代码。这意味着你有一个比赛条件。提出的最后一个请求不能保证是最后一个完成的请求。如果在最终请求完成时仍在加载其他脚本,您将看到此错误

您可以修改逻辑,以便只有在加载所有脚本后才执行回调。试试这个:

var requests = [];
$.each(arrFilesToLoad , function (key) {
    requests.push($.getScript(arrFilesToLoad[key]));
});

$.when.apply(this, requests)
    .done(startScript)
    .fail(function() {
        console.log('one or more scripts failed to load');
    });

function startScript() {
    var oContainer = createContainer();
    var oMainMenu = new Menu(arrMainMenu);
    $(oContainer).append(createMenu(oMainMenu));
    $('body').append(oContainer);
}

函数调用时尚未加载定义了
createContainer
的文件。是否在加载的脚本之一中定义了
createContainer()
?如果是这样,那么哪一个?@rorymcrossan必须在其中一个文件中定义它,因为它只发生8次中的1次。在代码中,
key
in done callback应该始终是每个
循环设置的最后一个。关闭可以解决问题。也就是说,Rory的答案会更好,就像一个魔咒一样,我已经刷新了好几次页面,每次都像我期望的那样显示内容。你能给我解释一下这段代码是如何工作的吗,因为我对JQuery还比较陌生。很高兴能帮上忙。这与原始代码类似,区别在于它将
$.getScript
返回的承诺对象存储在一个数组中,然后在
时提供给
$。这意味着不再为每个完成的请求执行
done()
回调,而是在所有请求完成后执行一次。我明白了,非常感谢您的快速及时响应。