This time I will bring you a detailed explanation of javscript's callback function(callback), what are the precautions when using javscript callback function (callback), the following is a practical case, let's take a look take a look.
Study the callback function. The English name of the callback function is: callback
Function is also an object. Take a look at the written description:
1. The function uses Function()ConstructorFunction object created.
2. The Function object contains a string , and the string contains the javascript code of the function
Look at a demo1:
var fun = function (x) { console.log(x) }; fun(100); //运行结果:100
The above code As you can know, the function can be an object, and parameters can be passed in the function (such as x in demo1). On the contrary, the parameters in the function can also be passed to a function
demo2:
function get(x, cb) { var a = 100; a = a + x; cb(a) } get(100, function (a) { console.log(a) }); //运行结果:200
As you can see in demo2, there are two parameters in the function get(), one is x and the other is cb, Among them, cb is a function. You can do something in the function cb()
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
##Detailed explanation of JavaScript timer
Events and callback functions of JavaScript running mechanism
The above is the detailed content of Detailed explanation of JavaScript callback function (callback). For more information, please follow other related articles on the PHP Chinese website!