JavaScript(JS) JS pass parameter to a settimeout() function

w‮i.ww‬giftidea.com

In JavaScript, the setTimeout() function is used to execute a piece of code after a specified delay in milliseconds. You can also pass parameters to the function that is called after the specified delay.

Here is an example of passing a parameter to the setTimeout() function:

function myFunction(param1, param2) {
  console.log("param1:", param1);
  console.log("param2:", param2);
}

// Call myFunction with parameters after 2 seconds
setTimeout(myFunction, 2000, "Hello", "World");

In this example, the myFunction() function takes two parameters param1 and param2. We pass these parameters to the setTimeout() function after the specified delay of 2000 milliseconds. The first parameter is the function myFunction, followed by the delay time, and then the parameters that we want to pass to myFunction.

When the timeout is complete, the myFunction() function will be called with the two parameters "Hello" and "World", which will be logged to the console.

Note that the parameters passed to setTimeout() after the delay time are optional. You can omit them if you don't need to pass any parameters to the function.