Javascript callback function example _ javascript tips-js tutorial

Source: Internet
Author: User
A callback function is a function called by a function pointer. The following example shows how to use a callback function: a callback function is a function called by a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to which it points, we will say this is a callback function.

The JS Api explains as follows: A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

Reason for using the callback function: you can separate callers from callers. The caller does not care who is called. All they need to know is that there is a called function with a specific prototype and certain restrictions (for example, the returned value is int.

Consider the following example:

Assume that the bottom layer and top layer of a project are completed by different personnel. the bottom layer is responsible for data access, and the top layer is responsible for data presentation. when the senior management needs to use the data of a certain module, he told the bottom-layer personnel that I need you to provide data that meets certain requirements and you provide me with an interface.

The bottom-layer Personnel said: I provide you with data. How to display and process it is your business. I cannot provide a data interface for every requirement, and I will provide you with a pass interface. you get the data and write the function to display it. after negotiation, both parties provide such an interface:

The Code is as follows:


// Data indicates the data source provided by the underlying layer, and funcName indicates the calling function of the high-level layer.

Function (data, funcName ){

1. data belongs to scenario 1, which is processed by the underlying layer;

2. data belongs to situation 2. What should I do if it is handled by a senior management? Use the function funcName provided by the top level for processing

.....

}


I may not have made it clear. Let's take a look at the example and we will understand it all at once.

The Code is as follows:


// Assume that the data source is an integer, which is the score of a student. When num <= 0, it is processed by the underlying layer, and when n> 0, it is processed by the high-level layer.

// Copy the following function and save it as 1.js.

Function f (num, callback ){
If (num <0 ){
Alert ("calls lower-level function processing! ");
Alert ("the score cannot be negative. incorrect input! ");
} Else if (num = 0 ){
Alert ("calls lower-level function processing! ");
Alert ("this student may not take the test! ");
} Else {
Alert ("calls high-level function processing! ");
Callback ();
}
}


The Code is as follows:


// Store the following test.html file and 1. js in the same directory:

Http://www.w3.org/TR/html4/loose.dtd>



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.