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.