This article is suitable for beginners of JavaScript.
Gossip first. In China, there is such a phenomenon: No matter what word or concept, there will always be some people who interpret these things as if they do not understand what they think they are very powerful. In fact, I don't know how many 2. I personally advocate conciseness and clarity. What are the words and concepts? If you can use a short sentence or two to let everyone understand, this is the skill. Now, let's get started with the topic.
What is Callback "Callback function" in JavaScript? In my words, Method B is used as a parameter to pass a method, after Method a is executed, execute another specified function (function B here ). Let's look at a column:
Copy codeThe Code is 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>
In main (B), B is called a callback function. Is it easy to understand? Why can't I put B's call into the main function? Yes, but then your main method can only call B. Here we pass B to a main, or a to a main. The callback method in main is a dynamic callback function. Here is the advantage.