简体   繁体   English

在加载另一个脚本之前,如何阻止脚本的执行?

[英]How can I block the execution of a script until another one is loaded?

Suppose I load these scripts from my host webpage : 假设我从主机网页加载这些脚本:

<script src="http://www.mywebsite.com/widget/widget.js?type=normal" type="text/javascript"></script>
<script src="http://www.mywebsite.com/widget/widget.js?type=rotation" type="text/javascript"></script>

and I'd like to execute the second one only when the first one have finished (totally; it can contain asynch functions). 并且我只想在第一个完成时执行第二个(完全;它可以包含异步函数)。

How can I do it? 我该怎么做?

You're already doing it right : the scripts are executed in the order of integration in the page, not loading. 您已经做得很好:脚本按照页面中的集成顺序执行,而不是加载。

EDIT : as MaxArt pointed, this may not be what you're looking for. 编辑:正如MaxArt指出的,这可能不是你想要的。 His answer is a good one. 他的答案很好。 But generally, you'll want to use javascript usual patterns and event based logic to avoid this kind of problems : 但一般来说,你会想要使用javascript常用模式和基于事件的逻辑来避免这种问题:

  • have most of your files define only classes and functions (some of them taking callbacks as parameters) 大多数文件只定义类和函数(其中一些文件将回调作为参数)
  • have a main file launching this and calling the sequence of actions, the asynchronicity being handled via callbacks. 有一个main文件启动它并调用动作序列,异步性通过回调处理。

My main code usually looks (a little) like this : 我的主要代码通常看起来像这样:

$(window).ready(function() {
    var myBigEngine = new BigEngine();
    myBigEngine.init(function(){
      // do other things, no need to add other layers as user events and ajax message callback will do the rest
    });
});

尝试将defer =“defer”属性应用于第二个<script>声明

<script src="http://www.mywebsite.com/widget/widget.js?type=rotation" type="text/javascript" defer="defer" ></script>

Wrap the whole script in a function, like this: 将整个脚本包装在一个函数中,如下所示:

(function(id) {
    var id = setInterval(function() {
        if (!window.doneLoading) return;
        clearInterval(id);

        // The whole script file goes here
        ...
    }, 50);
})();

The setInterval polls the (global, sorry) variable doneLoading . setInterval轮询(全局,抱歉)变量doneLoading In the first script, you have to set doneLoading to true or any other non-false value when your async function is completely loaded, like at the end of an AJAX request maybe? 在第一个脚本中,您必须在完全加载异步函数时将doneLoading设置为true或任何其他非假值,例如在AJAX请求结束时可能?

Edit : since I'm suggesting to add a global variable to the script, it may as well add a global function . 编辑 :因为我建议在脚本中添加一个全局变量,所以它也可以添加一个全局函数 So instead of setting up a setInterval call, wrap the second script inside a function... but like this: 因此,不是设置setInterval调用,而是将第二个脚本包装在函数中......但是像这样:

function doneLoading() {
        // The whole script file goes here
        ...
}

In the first script file, at the end of your callback function, just call doneLoading() . 在第一个脚本文件中,在回调函数结束时,只需调用doneLoading()

you can put second script init function in window.onload event 你可以在window.onload事件中放入第二个脚本init函数

window.onload = function(){

// call function from second script

}

or with jQuery 或者使用jQuery

$(function(){

// call function from second script

});

you can event add second script with this function 你可以使用此功能添加第二个脚本

function loadScript(src){
    var f=document.createElement('script');
    if(f){
        f.setAttribute("type","text/javascript");
        f.setAttribute("src",src);
        document.getElementsByTagName("head")[0].appendChild(f);
    }
}

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

相关问题 如何使JavaScript代码执行等到加载并执行带脚本的AJAX请求? - How can I get make JavaScript code execution to wait until an AJAX request with script is loaded and executed? A框架:如何等待执行,直到图像加载完毕? - A-frame: how can I wait with execution until image is loaded? 如何使脚本执行等到加载jquery - How to make script execution wait until jquery is loaded Firefox不执行一个动态加载的程序<script> element until another is loaded - Firefox doesn't execute one dynamically loaded <script> element until another is loaded 如何让 jquery 脚本在其可见部分加载之前不运行? (实际上在用户的屏幕上) - How can i make a jquery script NOT run until the visible part of it is loaded? (actually on the screen of the user) 如何强制一段代码等到 Typescript 中的 API 调用完成? - How can I force one block of code to wait until an API call finishes in Typescript? 延迟执行脚本直到前一个脚本完成 - Delay execution of a script until the previous one finishes 如何阻止执行直到执行异步调用? - how to block execution until Asynchronous call is executed? 如何在javascript中的另一个<script>中使用一个<script>的变量? - How can I use a variable of one <script> in another <script> in javascript? 推迟执行,直到在 Vue.js 中加载了外部脚本 - Defer execution until external script has loaded in Vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM