有没有什么方法可以控制Javascript异步加载顺序?

编程入门 行业动态 更新时间:2024-10-14 18:14:14
本文介绍了有没有什么方法可以控制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异步加载顺序?

本文发布于:2023-11-22 01:04:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有没有什么   顺序   加载   方法   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!