Javascript callback function
The concept of callback functions is always vague:
In the essence of JavaScript, the introduction to callback functions is as follows:
1. functions make it easier to process discontinuous events. For example, assume that there is such a sequence that is triggered by user interaction, and a request is sent to the server to display the server response. The most natural way to write is as follows:
request = prepare_the_request();response = send_request-synchronously(request);display(response);
2. The problem with this method is that synchronous requests on the network will cause the client to enter the suspended state. If the network transmission or server is slow, the response will be slow to unacceptable. A better way is to initiate an asynchronous request and provide a callback function that is triggered immediately 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. Once the response is received, it will be called immediately.
The above part actually explains the usage of the callback function. Next we will give an example. The simple description is to pass method a as a parameter to method mian, after the method mian is executed, execute another specified function (). Here, a is the callback function.
function main(callback) { alert("main fun"); callback(); } function a(){ alert("a fun"); } main(a);