javascript - Wait for scripts with type="module" to finish before running the next script - Stack Overflow

At the bottom of the body tag inside my html document I have 2 script tags with type="module"

At the bottom of the body tag inside my html document I have 2 script tags with type="module". Below that I have a script tag with some embedded code that depends on the result of the previous 2 scripts.
Is there a way to make sure that this code only gets executed once the first 2 scripts have finished?

<script src="./src/js/script1.js" type="module"></script>
<script src="./src/js/script2.js" type="module"></script>
<script type="text/javascript">
  // Code inside this tag should only run after the 2 previous script tags have been executed
  console.log('Hello SO');
</script>

At the bottom of the body tag inside my html document I have 2 script tags with type="module". Below that I have a script tag with some embedded code that depends on the result of the previous 2 scripts.
Is there a way to make sure that this code only gets executed once the first 2 scripts have finished?

<script src="./src/js/script1.js" type="module"></script>
<script src="./src/js/script2.js" type="module"></script>
<script type="text/javascript">
  // Code inside this tag should only run after the 2 previous script tags have been executed
  console.log('Hello SO');
</script>

Share Improve this question asked Jan 6, 2023 at 1:59 HendrikHendrik 6421 gold badge8 silver badges18 bronze badges 2
  • what do you mean by results? are the scripts running async code? – evolutionxbox Commented Jan 6, 2023 at 2:02
  • "I have a script tag with some embedded code that depends on the result of the previous 2 scripts" - that's what module dependencies are made for! Use import declarations – Bergi Commented Jan 6, 2023 at 8:27
Add a ment  | 

3 Answers 3

Reset to default 4

<script> tags with type="module" are automatically given a defer attribute (see load and execute order of scripts), so to make the third tag run after, it also needs to be deferred. Since this isn't possible with inline scripts (as mentioned in the ments), you can either move the script to another file and reference it with the src attribute or wrap the code in a event listener for the DOMContentLoaded event (see https://stackoverflow./a/41395202/19461620)

Using an external script file:

<script type="text/javascript" src="./script.js" defer></script>

script.js

  // Code inside this tag should only run after the 2 previous script tags have been executed
  console.log('Hello SO');

Using a DOMContentLoaded callback:

<script type="text/javascript" >
    window.addEventListener('DOMContentLoaded', function() {
        (function($) {
            //do something with b-lazy plugin, lightbox plugin and then with flexslider
        })(jQuery);
    });
</script>

Make your inline script a module one and import the required resources from there:

<script type="module">
  import result1 from "./src/js/script1.js";
  import result2 from "./src/js/script2.js";
  console.log('Hello SO');
</script>

Live example Source

You can make use of the onload callback of scripts to make sure the order is maintained.

let scriptsLoaded = 0;

//script1
var script1 = document.createElement('script');
script.type = 'module';
script.src ='./src/js/script1.js';
document.head.appendChild(script);

//script2
var script2 = document.createElement('script');
script.type = 'module';
script.src ='./src/js/script2.js';
document.head.appendChild(script);

script1.onLoad = function () {
    scriptsLoaded++;
    if(scriptsLoaded == 2) //load third script
}

script2.onLoad = function () {
    scriptsLoaded++;
    if(scriptsLoaded == 2) //load third script
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744795214a4594165.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信