Javascript callback function

Source: Internet
Author: User

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);  






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.