Home Web Front-end JS Tutorial JS callback async

JS callback async

Oct 20, 2016 pm 05:41 PM
js

Foreword

I am a newbie to the front-end. Although I have seen articles about callbacks before, it is a bit difficult to understand. There was only one concept in my mind at that time.

Callback: Mostly appear in Ajax requests and are used to process the received request results.

Hey, that was really my idea at the time. Now that I have really entered this industry, and this concept is also very important and used in too many places, it is time to pick it up and understand it carefully.

Of course, this article is suitable for novices, because I understand it from the perspective of a novice.

Callback concept

To understand a new thing, it is necessary to understand its concept, because this is the most concise and clear summary summarized by predecessors.

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

Chinese meaning: Callback is a function that is passed as an argument to another function and is executed in that function Execute after completion.
It’s a bit convoluted, okay, let’s speak plainly. That is, function B is passed to function A as a parameter, and function B is executed after function A is executed.

Let’s see how the code implements callbacks.

function A(callback){
    console.log("I am A");
    callback();  //调用该函数
}

function B(){
   console.log("I am B");
}

A(B);
Copy after login

This should be the simplest callback. I think everyone should understand the meaning of callback. Of course, such a simple synchronization callback code will not be used. In reality, it is relatively complicated to pass parameters.

Callback functions and asynchrony

I was a little confused by callbacks and asynchrony at first. I thought callbacks must be asynchronous.
In fact, this is not the case. I believe that in the above examples of functions A and B, we have understood that callbacks are not necessarily asynchronous. They themselves are not directly related.

Now we can understand synchronous callbacks and asynchronous callbacks (I won’t talk about synchronous and asynchronous separately, the concept is very simple).

Synchronous callbacks

are the above examples of A and B functions. They are synchronous callbacks.

Asynchronous callback

Because js is single-threaded, but there are many execution steps (ajax requesting remote data, IO, etc.) that are very time-consuming. If the single-thread is blocked all the time, the waiting time of the program will be too long. The page becomes unresponsive, affecting the user experience.

How to solve this problem? We can think about it this way. We will do all the time-consuming tasks asynchronously. When we are done, we will be notified that we are done, and we will get the data and continue.

var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);   //第三个参数决定是否采用异步的方式
    xhr.send(data);
    xhr.onreadystatechange = function(){
        if(xhr.readystate === 4 && xhr.status === 200){
                ///xxxx
        }
    }
Copy after login

The above is an ajax code. When the browser initiates an ajax request, it will open a separate thread to initiate the http request. In this way, this time-consuming process can be run independently. During the request process of this thread , the value of readystate will have a changing process, and each change will trigger the onreadystatechange function to determine whether the returned result is obtained correctly.

JS callback async

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Maps to implement map pan function

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

How to use JS and Baidu Map to implement map click event processing function

What does the new operator do in js What does the new operator do in js Nov 13, 2023 pm 04:05 PM

What does the new operator do in js

See all articles