问题描述
如何设置两个外部 async
Javascript文件的加载和执行顺序?
How do I set the load and execution order of two external async
Javascript files?
鉴于以下内容...
<script src="framework.js" async></script> // Larger file
<script src="scripts.js" async></script> // Small file
虽然排名第二 scripts.js
正在 framework.js
之前下载并执行,因为它的文件大小,但 scripts.js
依赖于 framework.js
。
Though second in order scripts.js
is downloading and executing before framework.js
due to it's file size, but scripts.js
is dependent on framework.js
.
有没有办法在指定加载和执行顺序的同时保持异步
属性?
Is there a way natively to specify the load and execution order whilst still maintaining async
properties?
推荐答案
您想要使用延迟
如果要保留执行顺序。什么推迟
确实是异步下载脚本,但推迟执行直到html解析完成。
You want to use defer
if you want to preserve the execution order. What defer
does is it async downloads the script, but defers execution till html parsing is done.
<script src="framework.js" defer></script>
<script src="scripts.js" defer></script>
但是,一旦脚本数量增加,您可能希望开始创建自定义捆绑包。
However, you may want to start creating custom bundles once the number of scripts go higher.
您可以看到差异
这篇关于有没有什么方法可以控制Javascript异步加载顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!