博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Async and Defer
阅读量:6424 次
发布时间:2019-06-23

本文共 1496 字,大约阅读时间需要 4 分钟。

async and defer

async

  There are actually two ways we can bypass the problem of the blocking script — async and defer.

  Async scripts will download the script without blocking rendering the page and will execute it as soon as the script finishes downloading.

  You get no guarantee that scripts will run in any specific order, only that they will not stop the rest of the page from displaying.

  It is best to use async when the scripts in the page run independently from each other and depend on no other script on the page.

For example, if you have the following script elements:

复制代码

  You can't rely on the order the scripts will load in. jquery.js may load before or after script2.js and script3.js and if this is the case, any functions in those scripts depending on jquery will produce an error because jquery will not be defined at the time the script runs.

defer

  Defer will run the scripts in the order they appear in the page and execute them as soon as the script and content are downloaded:

复制代码

  All the scripts with the defer attribute will load in the order they appear on the page.

  So in the second example, we can be sure that jquery.js will load before script2.js and script3.js and that script2.js will load before script3.js.

To summarize:

  • If your scripts don't need to wait for parsing and can run independently without dependencies, then use async.

  • If your scripts need to wait for parsing and depend on other scripts load them using defer and put their corresponding

转载地址:http://niwga.baihongyu.com/

你可能感兴趣的文章
Tiny210 U-BOOT(二)----配置时钟频率基本原理
查看>>
代理模式
查看>>
javaweb学习总结(二十四)——jsp传统标签开发
查看>>
让script的type属性等于text/html
查看>>
linux 文件系统sysvinit 流程分析
查看>>
体素科技:2018年,算法驱动下的医学影像分析进展
查看>>
Vue 折腾记 - (8) 写一个挺靠谱的多地区选择组件
查看>>
VS Code折腾记 - (3) 多图解VSCode基础功能
查看>>
『翻译』Node.js 调试
查看>>
我的iOS开发之路总结(更新啦~)
查看>>
Java NIO之拥抱Path和Files
查看>>
微信原图泄露的只能是 Exif ,你的隐私不在这!!!
查看>>
微信小程序教学第三章(含视频):小程序中级实战教程:列表篇-页面逻辑处理...
查看>>
页面间通信与数据共享解决方案简析
查看>>
Swift 中 Substrings 与 String
查看>>
作为一个开源软件的作者是一种什么样的感受?
查看>>
移动端适配知识你到底知多少
查看>>
TiDB 在 G7 的实践和未来
查看>>
重新认识javascript对象(三)——原型及原型链
查看>>
小学生学“数学”
查看>>