Skip to content

How can I pass a parameter to a setTimeout() callback? | Example

  • by

You can pass arguments to the function inside the setTimeout function.

Here is the syntax to pass a parameter to setTimeout() callback. The arg1, arg2, and arg3 are the arguments passed to the function.

The “setTimeout” receives a parameter that is sent as parameter in function end.

setTimeout(functionname, milliseconds, arg1, arg2, arg3...)

Pass parameter to setTimeout callback function example code

HTML example code of pass a parameter to a setTimeout() callback:-

1000 ms = 1 sec

We set 2 parameters you can set only one or all three.

<!DOCTYPE html>
<html>
<body>
	<button onclick="timeFunction()">Alert Me!</button>
	<script>
		function timeFunction() {
			setTimeout(alertFunc, 1000, " First param ", " Second param ");
		}
		function alertFunc(param1, param2) {
			alert(param1 + param2)
		}
	</script>
	<p>wait for 1 seconds.</p>
</body>
</html>

Output:

How can I pass a parameter to a setTimeout callback

Do comment if you have any doubts and suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *