JavaScript setTimeout() Method
Last Updated :
24 Apr, 2025
JavaScript setTimeout()
method
allows you to schedule the execution of a function or the evaluation of a code after a specified delay.
The setTimeout()
method calls a function after several milliseconds. setTimeout()
is for executing a function once after a specified delay.
Syntax:
setTimeout(function, delay);
Parameters:
function
: The function or code snippet to be executed after the specified delay.delay
: The time, in milliseconds, to wait before executing the function.
Return Value:
Returns a Number which is the id of the timer. Use this id with clearTimeout(id) to cancel the timer.
Example 1: Here, the greet
function will be executed after a delay of 2000 milliseconds (2 seconds).
JavaScript
function greet() {
console.log("Hello, world!");
}
// Call the greet function after
// 2000 milliseconds (2 seconds)
setTimeout(greet, 2000);