当前位置:网站首页>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 !!
边栏推荐
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
- C language input / output stream and file operation [II]
- Oracle EMCC 13.5 environment in docker every minute
- How to set encoding in idea
- Mujoco Jacobi - inverse motion - sensor
- 互动滑轨屏演示能为企业展厅带来什么
- DAY ONE
- [CVPR 2022] target detection sota:dino: Detr with improved detecting anchor boxes for end to end object detection
- [daily problem insight] prefix and -- count the number of fertile pyramids in the farm
- Article management system based on SSM framework
猜你喜欢

【vulnhub】presidential1

Mujoco Jacobi - inverse motion - sensor

什么是响应式对象?响应式对象的创建过程?

Mujoco second order simple pendulum modeling and control

JWT signature does not match locally computed signature. JWT validity cannot be asserted and should

2022年PMP项目管理考试敏捷知识点(9)

Data analysis course notes (V) common statistical methods, data and spelling, index and composite index

alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况

System activity monitor ISTAT menus 6.61 (1185) Chinese repair

uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
随机推荐
Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
Model-Free Prediction
准备好在CI/CD中自动化持续部署了吗?
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Command line kills window process
【vulnhub】presidential1
X.509 certificate based on go language
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Quickly use various versions of PostgreSQL database in docker
2022/2/12 summary
Three sentences to briefly introduce subnet mask
kubernetes部署ldap
Sword finger offer 26 Substructure of tree
Why should a complete knapsack be traversed in sequence? Briefly explain
Compilation of kickstart file
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
37頁數字鄉村振興智慧農業整體規劃建設方案
stm32F407-------DAC数模转换