当前位置:网站首页>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)
})
})
})边栏推荐
- Find command – find and search for files
- Try with resource close resource flow
- Soft exam information system project manager_ Management Science (Operations Research) 2--- senior information system project manager of soft test 034
- The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
- Day22 send request and parameterization using JMeter
- Lesson 9: workspace introduction
- Wireless industrial Internet of things data monitoring terminal
- [JS basic review] scope, this, closure
- Vscode voice notes to enrich information (Part 1)
- Classic usage of the sumproduct function
猜你喜欢

Getting started with mongodb

MySQL uses the where condition to find strange results: solve

Day22 send request and parameterization using JMeter

Mongodb delete data

Understanding the dynamic mode of mongodb document
Technology inventory: past, present and future of Message Oriented Middleware
SAP ui5 Application Development Tutorial Part 30 - parameter transfer in the routing process of SAP ui5

Rhcsa--- day 6 operation

ThreadLocal

Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
随机推荐
TFTP command – uploading and downloading files
Do you know what a three-tier architecture is?
Pat 1045 quick sort
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
By inserting a section break, the word header, footer, and page number can start from any page
MySQL uses the where condition to find strange results: solve
Soft exam information system project manager_ Management Science (Operations Research) -- senior information system project manager of soft test 033
JS implementation mouse can achieve the effect of left and right scrolling
Guava immutable set
Guava new collection type
What is the use of the subprocess module
Go uses channel to control concurrency
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
How the sap ui5 framework performs single step debugging of batch requests
Wind farm visualization: wind farm data
Simple student management system
Leetcode topic [array] -36- effective Sudoku
Some common errors and solutions of using SAP ui5 to consume OData services
cacacahe