The setTimeout()
method is used to call a function after a number of milliseconds (specified time). JavaScript settimeout with parameters are optional to pass to the function.
setTimeout(function, milliseconds, param1, param2, ...)
setTimeout(myFunc, 2000, "param1", "param2");
JavaScript settimeout with parameters
Simple example program to pass parameter to a setTimeout() function. Where the greet()
function is passed to the setTimeout()
and it called after 3000 milliseconds (3 seconds).
<!DOCTYPE html>
<html>
<body>
<script>
function greet() {
console.log('Hello world after 3000ms');
}
// passing parameter
setTimeout(greet, 3000);
console.log('This message is shown first');
</script>
</body>
</html>
Output:

Using additional parameters
<script>
function greet(a, b) {
console.log(a);
console.log(b);
}
setTimeout(greet, 3000, 'Hello', 'world');
console.log('This message is shown first');
</script>
Output:
This message is shown first Hello world
Use an anonymous function
setTimeout(function() {myFunc("param1", "param2")}, 2000);
Do comment if you have any doubts or suggestions on this JS set timeout topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.