繁体   English   中英

延迟脚本运行,直到所有资产都加载完毕

[英]Delay a script from running until all assets have loaded

简要说明

这并不像听起来那么基本,所以在您阅读并理解我想要做的事情之前,请不要跳到回答:-)。

我有一个名为SineMacula的对象,它包含一些像这样的基本函数(暂时忽略ready函数):

(function(global, $){

    // MOST CODE STRIPT OUT HERE */

    /**
     * Sine Macula Run
     * Makes it easy to write plugins for the Sine Macula library
     *
     * @param function callback
     */
    SM.run = run;
    function run(callback){
        // Call the function with the Sine Macula
        // and jQuery objects
        callback(SM, $);
    }

    /**
     * Sine Macula Ready
     * Executes code once the Sine Macula and jQuery
     * libraries are ready
     *
     * @param function callback
     */
    SM.ready = ready;
    function ready(callback){
        // Call the function with the Sine Macula
        // and jQuery objects
        jQuery(function($) {
            callback(SM, $);
        });
    }

    /**
     * Sine Macula Load
     * Load the Sine Macula Libraries and Plugins
     * into the current document
     *
     * The options:
     * - package: the package of libraries to load
     * - packageURL: a remote source to load the package details from
     * - libraries: any additional libraries to load
     *
     * @param object parameter The options for the Sine Macula load
     */
    SM.load = load;
    function load(options){
        // BUILD A QUERY HERE
        // ...
        // Complete the url by appending the query
        url = '//libraries.sinemaculammviii.com/'+query;        
        // Append the script tag to the end of the document
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        $('head')[0].appendChild(script);       
    }
})(this, this.jQuery);

load()函数通过将相关的脚本标签附加到页面的head来简单地加载页面的所有相关插件/库。

加载页面中的所有脚本都作为对名为run的函数的回调run 这可确保jQuerySineMacula都传递给插件。

问题

这就是问题所在,因为库正在加载到SineMacula对象中,无法检测它们是否已加载。

例如,如果其中一个库包含一个名为setDate()的函数插件,我运行:

SM.setDate()

这不一定有效,因为setDate()函数可能尚未加载到SineMacula对象中,因此它将返回“未捕获的类型错误...”。

任何人都可以建议很好地适应SineMacula.ready()函数以检测库是否存在?

请不要对jQuery.load()函数提出建议,我很清楚这一点。

我不希望解决方案是函数load()的回调,除非绝对必要。

我希望这是有道理的,如果不是让我知道,我会发布更多信息。 希望保持尽可能简短。

提前致谢

更新

忘了提到我在这里有一个测试页面,您可以其中查看我遇到的错误并更好地了解我在做什么:-)

如果我理解正确的,它调用用户SM.run()从他们的脚本,然后调用内SM.load()它加载库的其他部分。

因此,如果是这种情况,那么无论如何,他们的脚本将在加载库的任何其他部分之前完成执行。

我认为您需要做的是要求用户将他们的.run()与他们的其余代码放在一个单独的脚本中。 然后您可以使用document.write加载其他脚本。 这将导致它们加载并阻止下一个脚本,该脚本将包含用户的其余代码:

function load(options){
    // BUILD A QUERY HERE
    // ...
    // Complete the url by appending the query
    document.write('<scr' + 'ipt type="text/javascript" ',
                            ' src="//libraries.sinemaculammviii.com/' + query,
                            '"><\/scr' + 'ipt>');      
}

<script type="text/javascript" src="/path/to/your/lib.js"></script>
<script type="text/javascript">
    SineMacula.load('all');
</script>

<!-- The document.write will write the new script here, and it will be loaded
         syncronously, so it will block. -->

<script type="text/javascript">
    // code that uses the loaded library parts
</script>

鉴于缺少某些上下文,不确定我是否在回答您的问题,但您可能想使用Deferred s:

SM.ready = ready;
function ready(callback){
    var deferred = $.Deferred();
    // Call the function with the Sine Macula
    // and jQuery objects
    jQuery(function($) {
        deferred.resolve();
        if (typeof(callback) === "function") {
            callback(SM, $);
        }
    });
    return deferred;
}

现在您可以执行以下操作:

SM.ready().done(function() {
    // everything is now set.
});

暂无
暂无

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

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