Use the setTimeout and setinterval timers to execute functions that can pass parameters.

Source: Internet
Author: User

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 );


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: 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.