当前位置:网站首页>Pre knowledge of asynchronous operation
Pre knowledge of asynchronous operation
2022-06-25 06:04:00 【taoqidejingling】
Catalog
JS It's a single-threaded language
Why? JS It's a single-threaded language
JS It's a single-threaded language
- Only one task can be processed at a time
- Single thread means , All tasks need to be queued , The previous task is over , To perform the next task
- If the previous task took a long time , Then the latter task has to wait
- So ,JS Designers divide all tasks into two categories , Synchronous and asynchronous
Why? JS It's a single-threaded language
- As a browser scripting language ,JavaScript Its main purpose is to interact with users , And operation DOM
Synchronization task
- Only the previous task was completed , To perform the next task
Asynchronous task
- When the synchronization task is executed to a certain WebAPI Trigger asynchronous operation when , here The browser will open a separate thread to handle these asynchronous tasks
// Sync
const a = 2
const b = 3
console.log(a + b)
// asynchronous
setTimeout(() => {
console.log(a + b)
}, 1000)- Please think about the output below ?
console.log(1)
setTimeout(() => { // Asynchronous task , Put it in the task queue
console.log(2)
}, 0)
console.log(3)
// 1、3、2The following figure illustrates the execution process of synchronous and asynchronous tasks

Ajax request
Ajax Is asynchronous JavaScript and XML, It's about creating interactive 、 Web development technology of fast dynamic web application , Without reloading the entire page , Technology to update some web pages . Through a small amount of data exchange with the server in the background ,Ajax Asynchronous update of web pages . This means that you can load the entire page without reloading it , Update a part of the web page .
// establish XMLHttpRequest object
const url = 'http://jsonplaceholder.typicode.com/users'
let xmlhttp
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest()
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
// Send a request
xmlhttp.open('GET', url, true)
xmlhttp.send()
// Server response
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
const res = JOSN.parse(xmlhttp.responseText)
console.log(res)
}
}
/**
* XMLHttpRequest Object is used to exchange data with the server in the background
* onreadystatechange Event storage function
* readyState Available status
* 0 Request not initialized
* 1 Server connection established
* 2 Request received
* 3 Request processing
* 4 Request completed , And the response is ready
*/Callback Hell Back to hell
- Multi level nesting of functions is called “ Back to hell callback hell” perhaps “ Back to the abyss ”, It refers to nested functions in functions
- Top up ajax The request is encapsulated into a function
// hold ajax The requested data is returned by a callback function
function ajax(url, callback){
// send out ajax request
let xmlhttp
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest()
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
xmlhttp.open('GET', url, true)
xmlhttp.send()
// onreadystatechange Event storage function
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
const result = JSON.parse(xmlhttp.responseText)
callback(result)
}
}
}
const url1 = 'http://jsonplaceholder.typicode.com/users'
const url2 = 'http://jsonplaceholder.typicode.com/todos'
const url3 = 'http://jsonplaceholder.typicode.com/photos'
// Multi level nesting of functions is called “ Back to hell callback hell” perhaps “ Back to the abyss ”, It refers to nested functions in functions
ajax(url1, res => {
console.log(res)
ajax(url2, res => {
console.log(res)
ajax(url3, res => {
console.log(res)
})
})
})边栏推荐
- Lesson 8: FTP server setup and loading
- SAP ui5 date type sap ui. model. type. Analysis of date parsing format
- Distributed solar photovoltaic inverter monitoring
- Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
- DF command – displays disk space usage
- Introduction to MySQL test run test framework
- Leetcode topic [array] -36- effective Sudoku
- Some common errors and solutions of using SAP ui5 to consume OData services
- Get the first letter of Chinese phonetic alphabet in Excel and capitalize it
- What is the use of the subprocess module
猜你喜欢
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications

Incorrect dependency of POM file
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!

Jenkins installation and configuration

Use of MySQL variables

C simple operation mongodb
Go language map sorting (key/value sorting)
Linus' speech recordings, which were lost in 1994, were made public
Wind farm visualization: wind farm data

Understanding the dynamic mode of mongodb document
随机推荐
How do product managers get started? How do they learn when no one takes them?
The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
Go uses channel to control concurrency
Summary of 6 common methods of visual deep learning model architecture
50 days countdown! Are you ready for the Landbridge cup provincial tournament?
Distributed solar photovoltaic inverter monitoring
Uname command – displays system information
SAP ui5 application development tutorial XXIX - Introduction to routing and navigation functions of SAP ui5 trial version
What is hybrid web containers for SAP ui5
Optimal Parking
Yunda's cloud based business in Taiwan construction 𞓜 practical school
Introduction to the main features of kyma when the cloud native application runs
Use of MySQL variables
Trial version of routing history and routing back and history of SAP ui5
Find command – find and search for files
[JS basic review] scope, this, closure
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
What are the reasons why most webmasters choose Hong Kong site group servers?
Farewell to Lombok in 996
MySQL uses the where condition to find strange results: solve