What is callback
The code copy is as follows:
A callback function is a function called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not called directly by the implementer of the function, but is called by another party when a specific event or condition occurs, and is used to respond to the event or condition.
This explanation seems complicated, so I found a better explanation on Zhihu
The code copy is as follows:
You go to a store to buy things, and the things you want are not in stock, so you leave your phone number with the clerk. After a few days, the store has goods, and the clerk will call you, and then you go to the store to pick up the goods after you receive the call. In this example, your phone number is called the callback function, and you leave the phone to the clerk, which is called the registration callback function. The store later has stock, which is called the event that triggers the callback association. The clerk calls you, which is called the callback function, and when you go to the store to pick up the goods, it is called the response callback event. The answer is finished.
In Javascript:
The code copy is as follows:
Function A is passed as an argument (function reference) into another function B, and this function B executes function A. Let's say that function A is called a callback function. If there is no name (function expression), it is called an anonymous callback function.
In fact, it means passing the function as a parameter.
Javscript Callback
Throw all the complicated explanations above into the trash can~ and see what Callback is
What is Callback
In jQuery, the hide method is probably like this
The code copy is as follows:
$(selector).hide(speed,callback)
When using it,
The code copy is as follows:
$('#element').hide(1000, function() {
// callback function
});
We just need to write a simple function inside
The code copy is as follows:
$('#element').hide(1000, function() {
console.log('Hide');
});
There is a small comment in this: the Callback function is executed after the current animation is 100% completed. Then we can see the real phenomenon. When the element with id element is hidden, the Hide will be output in the console.
That means:
Callback is actually, when a function is executed, the function that is now executed is the so-called callback function.
Callback function
Under normal circumstances, functions are executed in order, but Javascript is an event-driven language.
The code copy is as follows:
function hello(){
console.log('hello');
}
function world(){
console.log('world');
}
hello();
world();
Therefore, under normal circumstances, they will be executed in sequence, but when the world event is executed for a long time.
The code copy is as follows:
function hello(){
setTimeout( function(){
console.log( 'hello' );
}, 1000 );
}
function world(){
console.log('world');
}
hello();
world();
Then this is not the case at this time. At this time, world will be output and hello will be output, so we need callback.
Callback instance
A simple example is as follows
The code copy is as follows:
function add_callback(p1, p2, callback) {
var my_number = p1 + p2;
callback(my_number);
}
add_callback(5, 15, function(num){
console.log("call " + num);
});
In the example we have a function of add_callback that receives three parameters: the first two are two parameters to be added, and the third is the callback function. When the function is executed, the addition result is returned and 'call 20' is output in the console.