This method is provided by Javascript which guarantees the execution of a set of code after a certain time limit. This does not mean that the provided set of code runs exactly after the provided delay but it won’t be executed until that time period has elapsed. The `setTimeout` function returns an ID, also known as timeout id. The setTimeout operation can be cancelled using this ID by calling `clearTimeout()` and passing the ID to it as an argument.
Syntax
JavaScript setTimeout() Method Syntax :
Parameters of JavaScript setTimeout() Method
Parameters :
functionRef
A function that is to be executed when the timer expires.
code
Using a different syntax, we may supply a string rather than a function that will be constructed and executed after the timeout expires. The same security issues that come with using eval() apply to this syntax, thus it’s generally not recommended.
delay (Optional)
This is the time, in milliseconds for which the timer have to wait before the specified function or code is executed. If this parameter is deleted, a value of 0 will be used, which means execute “immediately”, or more precisely, the next event cycle.
We should note that in either case, the actual delay can be longer than intended; We can see the Reasons for delays that are longer than specified below.
We should also note that if the value is not a number, implicit type coercion is done silently on the value for converting it to a number — this can possibly lead to unexpected and surprising results; We can see Non-number delay values that are coerced silently into numbers taken as example.
param1, …, paramN (Optional)
These are the additional arguments that are passed through to the function which are specified by rifunctionRef.
Return value
The returned timeoutID is supposed to be a positive integer value that identifies the timer created by the call to setTimeout().
It is generally guaranteed that a timeoutID value is never going to be reused by any consequent call to setTimeout() or setInterval() on the same object.
Example: