Whether it is window. setTimeout or window. setinterval, parameters cannot be included when using the function name as the call handle. In many cases, parameters must be included, which requires a solution. After query on the internet, sort the information as follows:
For example, for the hello (_ name) function
Welcome information:
VaR username = "Jack"; // display the welcome information according to the user name function Hello (_ name) {alert ("hello," + _ name );}
At this time, if you attempt to use the following statement to delay the hello function execution by 3 seconds, it is not feasible:
Window. setTimeout (Hello (username), 3000 );
This will enable the hello function to be executed immediately and pass the returned value to the setTimeout function as the call handle. The result is not required by the program. You can use a string to achieve the desired result:
Window. setTimeout ("Hello (username)", 3000); this is method (1)
The string here is a piece of JavaScript code, where username represents a variable. However, this method is not intuitive enough, and function names must be used in some cases, so some people think of the following:
Method 2 ):
<Script language = "JavaScript" type = "text/JavaScript"> <! -- Var username = "Jack"; // display the welcome information according to the user name function Hello (_ name) {alert ("hello," + _ name);} // create a function, returns a non-Parameter Function function _ Hello (_ name) {return function () {Hello (_ name) ;}} window. setTimeout (_ Hello (username), 3000); // --> </SCRIPT>
This defines a function _ Hello, which is used to receive a parameter and return a function without a parameter. In this function, external function parameters are used to call it, no parameters are required. In the window. setTimeout function, _ Hello (username) is used to return a function handle without parameters, thus implementing the function of passing parameters.
In addition, some users modify setTimeout and setinterval. That is, the following
Method 3:
<Script language = "JavaScript" type = "text/JavaScript"> <! -- Var username = "Jack"; // display the welcome information according to the user name. Function Hello (_ name) {alert ("hello," + _ name );} // * =================================================== ==========================================/// * function: modify window. setinterval to pass Parameters and object parameters // * method: setinterval (callback function, time, parameter 1, parameter n) parameters can be objects: for example, arrays, etc. // * = ====================================== VaR _ sto = setinterval; window. setinterval = function (callback, timeout, Param) {var ARGs = array. prototype. slice. call (arguments, 2); VaR _ cb = function () {callback. apply (null, argS);} _ STO (_ CB, timeout);} window. setinterval (hello, 3000, username );