JavaScript callback function

Source: Internet
Author: User

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

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.