
This is convenient when the function doesn't contain many lines of code.Īnonymous function means that you embed the code directly as the first parameter in setTimeout(), and don't reference the function name like you saw above. Overall, this means that setTimeout() will execute the code contained in a given function once, and only after a specified amount of time.Īt this point, it is worth mentioning that instead of passing a function name, you can pass an anonymous function to setTimeout(). It represents the specified amount of time the program has to wait for the function to be executed.

Since setTimeout() is part of the window object, it can also be written as tTimeout(). That timer indicates a specific moment when there will be a trigger for that particular action. The role of setTimeout(), an asynchronous method of the window object, is to set a timer that will execute an action. setTimeout vs setInterval - What is the difference?.Here is what we will cover in this quick guide: In this article, you will learn about the setTimeout() method – what it is and how you can use it in your programs.

Something like that is possible with JavaScript.

You may need certain lines of code to execute at a point in the future, when you explicitly specify, rather than all the code executing synchronously. Sometimes you might want to delay the execution of your code.
