且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用async和defer按顺序加载脚本

更新时间:2023-12-04 18:42:16

答案是,如果您使用 async defer ,您将失去对订单脚本执行的保证。

The answer is that you lose guarantees about the order scripts are executed if you use async or defer.

async 脚本是异步执行的,不能保证哪个顺序。 延迟脚本在解析文档后执行,但无法保证它们是否按顺序执行。 (具体来看关于延迟脚本的这个问题。)

async scripts are executed asyncronously, there are no guarantees as to which order there are. defer scripts are executed after the document has been parsed, but there are no guarantees as to whether they will be executed in order. (Have a look at this question about defer scripts specifically.)

不幸的是,在你的情况下,你必须通过删除 jquery.js >推迟和 async 属性。

Unfortunately, in your case, you have to run jquery.js synchronously by removing the defer and async attributes.

展望未来,当我们转向模块时,指定依赖关系并及时加载它们(只有一次)将变得更加容易。

Looking forward, as we move to modules, specifying dependencies and loading them just in time (and only once) will be made much easier.