Understanding and usage of callback functions in js, js callback Functions
Understanding and usage of callback functions in js
I. Functions of the callback function
The js Code goes down to the next line, but sometimes we need to wait until an operation ends before performing the next operation. At this time, we need to use the callback function.
Ii. Explanation of callback Functions
Because a function is actually an object, it can be stored in variables, passed to another function through parameters, created inside the function, and returned result values from the function ", because a function is a built-in object, we can pass it as a parameter to another function, execute it in the function, or even return it after execution, it has always been regarded as an obscure technology by "professional programmers.
The callback function is interpreted as follows:
A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.
The callback function is a function that is passed as a variable to another function. It is executed after the main function is executed.
Function A has function B. function B is called and executed after function A is executed.
Iii. Usage of callback Functions
The Code is as follows:
Function a (callbackFunction ){
Alert ("this is parent function ");
Var m = 1;
Var n = 3;
Return callbackFunction (m, n );
}
Function B (m, n ){
Alert ("this is callback function B ");
Return m + n;
}
$ (Function (){
Var result = a (B );
Alert ("result =" + result );
});
The execution sequence is as follows:
This is the parent function.
This is the callback function B.
Result = 4
The function first executes theme function a, then calls callback function B, and finally returns the return value of function.