当前位置:网站首页>Three methods to realize JS asynchronous loading
Three methods to realize JS asynchronous loading
2022-07-07 00:35:00 【Cut potatoes into shreds】
One 、 Why write Load asynchronously :
①js Loading itself belongs to Synchronous loading Of , load js Document meeting Blocking documents , Once the network speed is bad , So the whole site will wait js Loading without subsequent rendering, etc . But some tools and methods need Load on demand , There are some tools js File it will not change the page , Use reload , No need not load .
② But in the actual development, we only need to put script The tag is placed at the bottom of the page . There is no need to write Redundant code , But loading on demand must be the top priority of the interview !!!
Two 、JS Load asynchronously There are three solutions to this problem :
①defer
script Add... To the label defer attribute , Load asynchronously
1. But wait dom All documents have been parsed (dom Tree generated ) To be executed .
2. Only IE It works ;
②async
script Add... To the label async attribute , Load asynchronously
1. Execute after loading ;async Only external scripts can be loaded
2. Can't take js Written in script In the label .
3.w3c standard ,IE9 The following is not supported
③ Encapsulate a function compatible asynchronous load js File, and the functions in the file can be executed on demand ( Load on demand )
<script>
function loadScript(url,callback){
//url It's loaded on demand js file
//callback It's loaded on demand js A function in the file
// 1. Create a script label
var script = document.createElement('script');
// Handle ie Compatibility
if(script.readyState){
script.onreadystatechange = function(){
// If script Download completed
if(script.readyState == 'complete' || script.readyState == 'loaded'){
callback();
}
}
}else{
// monitor script Download status of When the status changes to download complete Re execution callback
script.onload = function(){
callback();
}
}
// The purpose introduced later is if IE If the download is too fast ( Faster than reading programs )
//IE Of readystatechange When the event detects the status code , It's already from loading become complete perhaps loaded( Finished loading the file at a very fast speed , You haven't had time to test )
// Then if you test it again, it won't change , It's always been complete perhaps loaded
// This time is a hindsight , The test is useless .
// 2. to script add src The introduction of a js file
script.src = url;
// 3. Append to body
document.body.appendChild(script);
}
</script>
3、 ... and 、 Finally added : document Three states
①loading Loading status ,dom The tree is being drawn
②interactive Active state ,dom Tree drawing complete
③complete Completion status ( Older browsers may be loaded),dom The tree drawing is completed and all resources are downloaded
This is recorded for future use , I also hope the big guys can communicate more , Leave more messages , Point out my shortcomings !
You can study it if you need it !!
边栏推荐
- Data operation platform - data collection [easy to understand]
- [CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection
- 专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
- 准备好在CI/CD中自动化持续部署了吗?
- 如何判断一个数组中的元素包含一个对象的所有属性值
- Model-Free Control
- 互动滑轨屏演示能为企业展厅带来什么
- Cross-entrpy Method
- Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
- iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
猜你喜欢
ZYNQ移植uCOSIII
Racher integrates LDAP to realize unified account login
ldap创建公司组织、人员
X.509 certificate based on go language
2022 PMP project management examination agile knowledge points (9)
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
37页数字乡村振兴智慧农业整体规划建设方案
沉浸式投影在线下展示中的三大应用特点
AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解
随机推荐
华为mate8电池价格_华为mate8换电池后充电巨慢
PostgreSQL highly available repmgr (1 master 2 slave +1witness) + pgpool II realizes master-slave switching + read-write separation
Quickly use various versions of PostgreSQL database in docker
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
How to judge whether an element in an array contains all attribute values of an object
MySQL learning notes (mind map)
什么是响应式对象?响应式对象的创建过程?
Business process testing based on functional testing
2022/2/10 summary
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Core knowledge of distributed cache
Mujoco produces analog video
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
数据运营平台-数据采集[通俗易懂]
Cross-entrpy Method
48页数字政府智慧政务一网通办解决方案
rancher集成ldap,实现统一账号登录
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
How engineers treat open source -- the heartfelt words of an old engineer
37 page overall planning and construction plan for digital Village revitalization of smart agriculture