JS callback function--easy to understand there are instances

Source: Internet
Author: User

Beginner JS, the callback function is very dizzy, now back to summarize what is the callback function. Let's take a look at the definition of the callback in English: A callback is a function, a passed as an argument to another function and is executed after its parent   function has completed. In a literal sense, a callback function is a parameter that passes the function as a parameter to another function, and then executes the function passed in when the function is executed.   This process is called a callback. In fact, it is very well understood, right, callback, callback, is called back to the meaning. The main function is done in advance, and then the function passed in is called back.   But I've seen a lot of blogs before, and they always explain the callback function in a foggy, very advanced way. Take an example: when you send your girlfriend home at the end of the date, when you leave, you'll say, "I'm worried about you when I get home and give me a message." "No, then your girlfriend really sent you a message when she came home. Boy, you have a chance. In fact, this is the process of a callback. You left a parametric function (asking your girlfriend to give you A clockwork message) to your girlfriend, and then your girlfriend goes home, and the action to go home is the main function.   She had to get home first. After the main function is finished, the function passed in is executed, and then you receive a message. Now basically understand the meaning of the callback function. It doesn't matter if you don't understand, we talk in code. Copy code//define main function, callback function as parameter function A (callback) {callback (); Console.log (' I am the main function ');}//define callback function B () {setTimeout ("Conso Le.log (' I am the callback function ') ', 3000);//simulate time-consuming operation}//Call the main function and transfer function B into A (b);   Output I am the main function I am the callback function copy the code above, we define the main function and the callback function, and then call the main function, the callback function passed in. When defining the main function, we let the code execute the callback () callback function first, but the output is the content of the output callback function. This means that the main function does not have to wait for the callback function to complete, and then executes its own code. So the general callback function is used on the time-consuming operation. such as Ajax requests, such as processing files. PS: The original is transferred from http://www.cnblogs.com/lishuxue/p/5999682.html

JS callback Function--easy to understand with instance

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.