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.
Example:
// Display a text using setTimeout method
function displayText() {
console.log('Hello world');
}
setTimeout(displayText, 3000);
console.log('This message is shown first');
This message is shown first
Hello world
In the program above, after 3000 milliseconds, the method setTimeout() executes the function displayText().