All along, the concept of a callback function is ambiguous:
The following is an introduction to the callback function in the JavaScript language's pristine book:
1. Functions make it easier to handle discontinuous events. For example, suppose you have a sequence that is triggered by user interaction, sends a request to the server, and finally shows the server's response. The most natural way to do this might be:
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 go into suspended animation, and if the network transmission or server is slow, the response is slow and unacceptable. A better approach is to initiate an asynchronous request that provides a callback function that fires when the server's response arrives. The asynchronous 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
In fact, this part has explained the use of the callback function, and then for another example, the simple point is to pass method A as a parameter to the method mian, when the method Mian executes after executing 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