简体   繁体   English

在js文件中加载javascript文件。 检查所有文件是否已加载的最佳方法是哪种?

[英]Loading javascript files in js files. Which is best way to check whether all files are loaded or not?

I have a array where i have specified the files i need to load in javascript before calling specific script. 我有一个数组,我在调用特定脚本之前已指定需要在javascript中加载的文件。 Lets call those particular lines of code as myscript . 让我们将这些特定的代码行称为myscript

I did as follows 我做了如下

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++;
        });
    });

to check whether all files are being loaded or not, i done following thing but doesn't seems to be effective 检查是否所有文件都被加载,我做了以下事情,但似乎并不有效

var refreshIntervalId = setInterval(function() {
        if (tempCount == totalFiles) {
            clearInterval(refreshIntervalId);
            return;
        }
    }, 10);

i have implemented these in object oriented javascript as follows 我已经在面向对象的javascript中实现了这些,如下所示

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
}

i am loading more than 12-14 js files via ajax. 我通过ajax加载了12-14个以上的js文件。

if you observe Loader.prototype.bindMap , i am loading all the files first and then executing the script. 如果您观察到Loader.prototype.bindMap ,那么我将首先加载所有文件,然后执行脚本。

But it seems that myscript the script start executing before all files being loaded. 但是似乎myscript脚本在加载所有文件之前开始执行。

what are the better ways to execute the script only after all js files are loaded. 只有在加载所有js文件之后执行脚本的更好的方法是什么?

看看jQuery的.load() http://api.jquery.com/load-event/

$('script').load(function () { }); 

Based on the documentation on Jquery.getScript , it is a shorthand for Jquery.ajax. 根据Jquery.getScript的文档,它是Jquery.ajax的简写。 By default this in async call. 默认情况下,这在异步调用中。 You might want to change it to do a synchronous call. 您可能需要更改它以进行同步调用。 To set this property, you can refer to this 要设置此属性,您可以参考

So instead of doing a setInterval, you can just loop in your array and do a Jquery.getScript. 因此,除了执行setInterval之外,您还可以在数组中循环并执行Jquery.getScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM