setTimeout() is a JavaScript function that allows you to schedule a function to be executed after a specified amount of time (in milliseconds).
Here’s an example usage:
// declare a function to be executed
function greeting() {
console.log("Hello, World!");
}
// schedule the function to be executed after 2 seconds (2000 milliseconds)
setTimeout(greeting, 2000);
In this example, the greeting function will be executed 2 seconds after the setTimeout() function is called.
Here’s another example that demonstrates how setTimeout() can be used with an anonymous function:
// schedule an anonymous function to be executed after 3 seconds (3000 milliseconds)
setTimeout(function() {
console.log("The timeout has expired!");
}, 3000);
In this example, the anonymous function will be executed 3 seconds after the setTimeout() function is called, and it will log the message “The timeout has expired!” to the console.