Callback functions are often used in many common jquery methods. After understanding the definition and usage of the JS callback function, we can use the callback function to help us do a lot of things!
A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.
This is the parsing of the callback function by JS. It means that the callback function is a function that is passed to another function in the form of parameters, and this function (refers to the callback function) it is called only after another function is executed! (When called, another function is the parent function of the callback function ).
It may be a bit difficult to understand. A common example is as follows:
Function A has a parameter, which is function B. function B is executed after function a is executed. This process is called callback.
One point that must be emphasized here: function B is your ParameterData formatIf it is passed to function a, function B is called a callback function when called.
Question:
Must a function be passed in the form of parameters? can function B be called directly in function a to implement the callback function?
Answer:
Using a function for parameters has the following benefits: When a (B) is used, function B becomes the callback function, and you can also use a (c, function C becomes the callback function. If you call this function directly in function A, the callback function will be suspended. If you write function a () {...; B () ;}, the flexibility of the variable is lost.
Here is a simple example:
<SCRIPT>FunctionA (callback) {alert ("The main function is finished! "); Callback ();}FunctionB () {alert ("Callback function B! ");}FunctionC () {alert ("Callback Function C! ");}//The callback function is passed as a parameter to ensure variable flexibility.A (B); A (c );</SCRIPT>