Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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
在js文件中加载javascript文件。检查是否加载了所有文件的最佳方法是什么?_Javascript_Ajax_Jquery_Loading - Fatal编程技术网

在js文件中加载javascript文件。检查是否加载了所有文件的最佳方法是什么?

在js文件中加载javascript文件。检查是否加载了所有文件的最佳方法是什么?,javascript,ajax,jquery,loading,Javascript,Ajax,Jquery,Loading,我有一个数组,其中指定了在调用特定脚本之前需要在javascript中加载的文件。让我们将这些特定的代码行称为myscript 我是这样做的 var fileNamesArray = new Array(); fileNamesArray.push("abc.js"); fileNamesArray.push("pqr.js"); fileNamesArray.push("xyz.js"); fileNamesArray.push("klm.js");

我有一个数组,其中指定了在调用特定脚本之前需要在javascript中加载的文件。让我们将这些特定的代码行称为
myscript

我是这样做的

var fileNamesArray = new Array();
    fileNamesArray.push("abc.js");
    fileNamesArray.push("pqr.js");
    fileNamesArray.push("xyz.js");
    fileNamesArray.push("klm.js");

    var totalFiles = jQuery(fileNamesArray).length;
    var tempCount = 0;

    jQuery.each(fileNamesArray, function(key, value) {
        jQuery.getScript(value, function() {
            tempCount++;
        });
    });
function Loader() {

    this.jQuery = null;

    // check for specifically jQuery 1.8.2+, if not, load it

    if (jQuery == undefined) {
        jQuery.getScript(
                "/Common/javascript/jquery/map/javascript/jquery-1.8.2.js",
                function() {
                    this.jQuery = jQuery.noConflict();
                });
    } else {
        var jQueryVersion = $.fn.jquery;

        jQueryVersion = parseInt(jQueryVersion.split('.').join(""));

        if (182 > jQueryVersion) {
            jQuery.getScript(
                    "/Common/javascript/jquery/map/javascript/jquery-1.8.2.js",
                    function() {
                        this.jQuery = jQuery.noConflict();
                    });
        }
    }
}

Loader.prototype.LoadAllFile = function() {
//here i am loading all files
}

Loader.prototype.bindMap = function(options) {
this.LoadAllFile();
//execute the script after loading the files... which we called as myscript
}
为了检查是否所有的文件都被加载,我做了下面的事情,但似乎没有效果

var refreshIntervalId = setInterval(function() {
        if (tempCount == totalFiles) {
            clearInterval(refreshIntervalId);
            return;
        }
    }, 10);
我已经在面向对象javascript中实现了这些,如下所示

var fileNamesArray = new Array();
    fileNamesArray.push("abc.js");
    fileNamesArray.push("pqr.js");
    fileNamesArray.push("xyz.js");
    fileNamesArray.push("klm.js");

    var totalFiles = jQuery(fileNamesArray).length;
    var tempCount = 0;

    jQuery.each(fileNamesArray, function(key, value) {
        jQuery.getScript(value, function() {
            tempCount++;
        });
    });
function Loader() {

    this.jQuery = null;

    // check for specifically jQuery 1.8.2+, if not, load it

    if (jQuery == undefined) {
        jQuery.getScript(
                "/Common/javascript/jquery/map/javascript/jquery-1.8.2.js",
                function() {
                    this.jQuery = jQuery.noConflict();
                });
    } else {
        var jQueryVersion = $.fn.jquery;

        jQueryVersion = parseInt(jQueryVersion.split('.').join(""));

        if (182 > jQueryVersion) {
            jQuery.getScript(
                    "/Common/javascript/jquery/map/javascript/jquery-1.8.2.js",
                    function() {
                        this.jQuery = jQuery.noConflict();
                    });
        }
    }
}

Loader.prototype.LoadAllFile = function() {
//here i am loading all files
}

Loader.prototype.bindMap = function(options) {
this.LoadAllFile();
//execute the script after loading the files... which we called as myscript
}
我正在通过ajax加载超过12-14个js文件

如果观察
Loader.prototype.bindMap
,我将首先加载所有文件,然后执行脚本

但似乎
myscript
脚本在加载所有文件之前开始执行


只有在加载所有js文件后才执行脚本的更好方法是什么。

看看jQuery的.load()


基于上的文档,它是Jquery.ajax的简写。默认情况下,这是一个异步调用。您可能希望将其更改为执行同步调用。 要设置此属性,可以参考


而不是做一个SET间隔,你可以在你的数组中循环并做一个jQuery .GestScript .< /P>你可以在这里找到答案:听起来你应该考虑一个模块加载器,例如:代码> Realjs < /Cube >或者<代码> CURL< /COD> @ R3MUS,我不能用<代码> /COD>在头部分或正文部分加载文件。只需在JS中加载所有文件。从Jquery1.8开始,此方法已被弃用。必须改用“on”方法