JavaScript callback function

Source: Internet
Author: User

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

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.