JavaScript callback (callback) function concept self-understanding and example

Source: Internet
Author: User

This article is suitable for JavaScript entry-level contestants to read, the master can float over.

Just a little gossip. In China, there is a phenomenon: no matter what words or concepts, there will always be some people to explain these things to the marvellous, it seems to make people do not understand that they feel very powerful appearance. Actually do not know how many 2. I personally advocate simplicity and clarity. What words, what concepts, if you can use a short one or two words to let people understand, this is the ability. All right, gossip's over, get to the point.

What is called callback "callback function" in JavaScript, in my words, is to pass method B as a parameter, and then execute another specified function (b function) When method A is done. Take a look:

Copy Code code as follows:


<html>


<head>


<title>callback function Test</title>


<script language= "javascript" type= "Text/javascript" >


function Main (callback)


{


alert ("I am Main function");


alert ("Invoke callback function ...");


callback ();


}


function B () {


alert ("I am Callback Function:b");


}


function C () {


alert ("I am Callback Function:c");


}





function Test () {


Main (b);


main (c);


}





</script>


</head>


<body>


<button onclick= "test ()" >click me</button>


</body>


</html>


This inside main (b), then B is called the callback function, is it good to understand? Then someone would say why can't I put B's call into the main function? Sure, but then your main method can only call B. Here we pass B a main, or a pass a main, the callback method in main is the dynamic callback function. The advantage is right here.

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.