All the time, the concept of a callback function is very vague:
The following is an introduction to the callback function in the JavaScript language pristine book, for example:
1. Functions make it easier to handle discontinuous events. For example, suppose there is such a sequence. Triggered by user interaction, a request is sent to the server. Finally, the server response is displayed.
The most natural wording could be this:
Request = Prepare_the_request (); response = send_request-synchronously (Request);d Isplay (response);
2. The problem with this approach is that a synchronization request on the network causes the client to enter the suspended animation state. Assume that the network transport or server is very slow. The response is too slow to be acceptable. A better approach is to initiate an asynchronous request that provides a callback function that fires when the server's response arrives. The async function returns immediately so that the client is not blocked.
Request = Prepare_the_request (); send_request-synchronously (request,function (response) { display (response);});
3. We pass a function as a parameter to the send_request-synchronously function, and once the response is received, it is immediately called
This part actually explained the use of the callback function, and then give a sample, the simple point is to pass method A as a parameter to the method mian, when the method Mian run after running another specified function (a). Here A is the callback function
Function Main (callback) { alert ("Main fun"); Callback (); } function A () { alert ("a fun"); } Main (a);
JavaScript callback function