What is callback
Copy Code code as follows:
A callback function is a function called by a function pointer. If you pass the pointer (address) of a function to another function as an argument, when the pointer is used to call the function it points to, we say this is a callback function. A callback function is not invoked directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
The explanation looked complicated and found a better explanation.
Copy Code code as follows:
You go to a store to buy things, just what you want is not the goods, so you left your phone at the shop assistant, after a few days in the store, the clerk called your phone, and then you received the phone to the store to pick up the goods. In this example, your phone number is called a callback function, you leave the phone to the clerk called the registration callback function, the store later the goods are called triggered the callback associated events, the clerk called you call callback function, you go to the store to fetch goods called response callback event. The answer is complete.
In javascript:
Copy Code code as follows:
Function A is passed to another function B as an argument (a function reference), and this function B executes function A. Let's just say function A is called a callback function. If there is no Name (function expression), it is called an anonymous callback function.
In fact, the function is passed as a parameter.
Javscript Callback
Throw the complicated explanations above into the trash can and see what callback is.
What's callback?
In jquery, the method of hide is probably like this
Copy Code code as follows:
$ (selector). Hide (Speed,callback)
Use of the time,
Copy Code code as follows:
$ (' #element '). Hide (1000, function () {
callback function
});
We just need to write a simple function inside.
Copy Code code as follows:
$ (' #element '). Hide (1000, function () {
Console.log (' Hide ');
});
There's a little annotation in this one: the Callback function executes after the current animation 100% completes. Then we can see that the real phenomenon is that when elements with ID element are hidden, the hide is exported in the console.
It also means:
Callback actually, when a function is done, the function that is being executed is the so-called callback function.
callback function
Normally, functions are executed sequentially, but JavaScript is an event-driven language.
Copy Code code as follows:
function Hello () {
Console.log (' Hello ');
}
function World () {
Console.log (' World ');
}
Hello ();
World ();
So it's normally done sequentially, but when the world event is running for a long time.
Copy Code code as follows:
function Hello () {
settimeout (function () {
Console.log (' Hello ');
}, 1000);
}
function World () {
Console.log (' World ');
}
Hello ();
World ();
This is not the case at this time, it will output world, and then output hello, so we need to callback.
Callback Instance
A simple example is as follows
Copy Code code as follows:
function Add_callback (P1, p2, callback) {
var my_number = p1 + p2;
Callback (My_number);
}
Add_callback (5, function (num) {
Console.log ("call" + num);
});
In the example we have a add_callback function that receives three parameters: the first two are the two arguments to add, and the third is the callback function. When the function executes, returns the result of the addition and outputs ' call 20 ' in the console.