The invocation between JS methods--The method of parameter transfer

Source: Internet
Author: User

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

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.