有没有什么方法可以控制Javascript异步加载顺序? [英] Any way to control Javascript Async Load Order?

查看:96
本文介绍了有没有什么方法可以控制Javascript异步加载顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置两个外部 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异步加载顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆