The global setTimeout() method initiates a timer, and when it expires, it executes a specified function or block of code.
JavaScript engine generates a new function and adds it to the call stack when the method setTimeout() is called.
Web APIs component of the web browser conducts setTimeout() and commences a timer. The callback function supplied to the method setTimeout() is added to the queue after the timeout expires.
- setTimeout(function, milliseconds);
Here, milliseconds is the amount of time it takes for a function to be executed and function is a block of code
Syntax
Example:
// Display a text using setTimeout method
function displayText() {
console.log('Hello world');
}
setTimeout(displayText, 3000);
console.log('This message is shown first');
Output
This message is shown first
Hello world
In the program above, after 3000 milliseconds, the method setTimeout() executes the function displayText().
Post a Comment