In recent project requirements found that the completion of a number of functions are always to adjust a lot of similar structure of the method, writing is very cumbersome, so you want to write a "universal" method, the code is more concise. That is: Pass a method as a parameter to this "omnipotent" method, let it execute your given method, just like the Success/error method in Ajax, always pass in a method body.
# # # #1. Use method as parameter
Code:
var a = function () {
Console.info ("I have successfully executed ...");
}
var excutefunc = function (funcName) {
FuncName ();
}
Excutefunc (a);
Console output:
I have successfully executed ...
Above we perform a method as a parameter to the Excutefunc method, using Excutefunc to execute a method, but if the A method also contains parameters, see the following example
# # # #2. Methods with parameters as parameters
Code:
var B = function (b) {
Console.info ("I succeeded in executing" +b);
}
var setfunc = function (fname,p) {
FName (P);
}
Setfunc (b, "B method");
Console output:
I managed to execute the B method.
The first argument to the Setfunc method in the previous example is the handle to the method to invoke, and the second parameter is the parameter to invoke the method, if more than one is listed in sequence
# # #3一个定时刷新数据的使用场景
var getData = function (URL) {
$.post (url,function (Result) {
Logic code ...
});
}
var getData1 ...
.
.
.
var Settimefresh = function (Fname,time,url) {
SetInterval (Fname,time,url);
}
Settimefresh (Getdata,3000,url);//3 seconds get Data refresh page
Settimefresh (GETDATA1,10000,URL1);
The previous example calls the Settimefresh method to refresh the specified data periodically to achieve the function of timed refresh of part of the page data.
Cabbage again to caught dead, do not blame!
The invocation between JS methods--The method of parameter transfer