What is the callback function CallBack

Explanation one, easy to understand:
Generally, when you write a program, you call the system's API. If you reverse the relationship, you write a function and let the system call your function, then it is a callback, and the function called by the system is the callback function.

Explanation two, easy to understand:
generally a function calls another function, the called function appears in the method body, and the callback function is more special, it appears in the parameter list. In other words, when calling, need Get this (callback) function from other places and pass it in as a parameter.

Explanation three, after reading the more specific answer: the
callback function is called callback function, in fact, a better definition is "call after" function.

Let me explain in human terms, a callback function is a function that will be executed immediately after another function finishes executing . A callback function is a function passed as a parameter to another JavaScript function. This callback function will be executed inside the passed function.

To quote a better note on Stack Overflow:

A callback function is a function which is:accessible by another
function, and is invoked after the first function if that first
function completes

The following two examples will make it clear:

function greeting(name) {
    
    
  alert('Hello ' + name);
}
 
function processUserInput(callback) {
    
    
  var name = prompt('请输入你的名字。');
  callback(name);
}
 
processUserInput(greeting);

The above example is a synchronous callback, which is executed immediately.

However, it should be noted that callback functions are often used to continue to perform an asynchronously completed operation, and they are called asynchronous callbacks.

another example:

// A function which accepts another function as an argument
// (and will automatically invoke that function when it completes - note that there is no explicit call to callbackFunction)
function printANumber(number, callbackFunction) {
    
    
    console.log("The number you provided is: " + number);
    setTimeout(function() {
    
     callbackFunction(); }, 3000); 
    console.log("second");
}
 
// a function which we will use in a driver function as a callback function
function printFinishMessage() {
    
    
    console.log("I have finished printing numbers.");
}
 
// Driver method
 
printANumber(6, printFinishMessage);

After executing the printANumber function, the result:

> "The number you provided is: 6"
> "second"
> "I have finished printing numbers."

You can see that "The number you provided is: 6" and "second" are output first, and "I have finished printing numbers." is output after a delay of three seconds.

So we can think that the actual execution of callbackFunction is executed after the method body of the main function body printANumber is executed.

This is of great significance to us. For example, when we call an external API in a webpage-related function, we must execute it asynchronously, and then pass in the callback function. If it is executed synchronously, it is blocking. We need to wait for the result of calling the API before executing. The latter function, this may cause the interface loading to freeze and delay, and the use of the callback function to execute asynchronously will not affect the execution of our later function and is non-blocking.

Guess you like

Origin blog.csdn.net/nikyae/article/details/111239334