setTimeout is a global JavaScript method, used to execute a particular function or piece of code after a given delay.
setTimeout
is a JavaScript function, which executes a piece of code after a given delay.
The delayed code has to be defined via the first argument. This can be a function reference, or a plain string. Passing a string is not recommended, for the same reasons that the use of eval
is discouraged.
The delay is defined in milliseconds. In all browsers except for Internet Explorer 9 and earlier, additional parameters can be added, which are passed to the function.
setTimeout
returns a timeout ID, which can be passed to clearTimeout
to cancel the execution of the delayed function.
To perform an action repetitively at an interval, use setInterval
and clearInterval
.