What is the callback function? How can I use the callback function?

Source: Internet
Author: User

Callback functions are often used in many common jquery methods. After understanding the definition and usage of the JS callback function, we can use the callback function to help us do a lot of things!

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

This is the parsing of the callback function by JS. It means that the callback function is a function that is passed to another function in the form of parameters, and this function (refers to the callback function) it is called only after another function is executed! (When called, another function is the parent function of the callback function ).

It may be a bit difficult to understand. A common example is as follows:

Function A has a parameter, which is function B. function B is executed after function a is executed. This process is called callback.

One point that must be emphasized here: function B is your ParameterData formatIf it is passed to function a, function B is called a callback function when called.

Question:

Must a function be passed in the form of parameters? can function B be called directly in function a to implement the callback function?

Answer:

Using a function for parameters has the following benefits: When a (B) is used, function B becomes the callback function, and you can also use a (c, function C becomes the callback function. If you call this function directly in function A, the callback function will be suspended. If you write function a () {...; B () ;}, the flexibility of the variable is lost.

Here is a simple example:

 
<SCRIPT>FunctionA (callback) {alert ("The main function is finished! "); Callback ();}FunctionB () {alert ("Callback function B! ");}FunctionC () {alert ("Callback Function C! ");}//The callback function is passed as a parameter to ensure variable flexibility.A (B); A (c );</SCRIPT>

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.