setTimeout — Executes a code snippet or a callable after specified delay.


setTimeout(mixed executable, [integer delay, [mixed param, …]])


Name Description Type Default Optional
executable Callable name or code snippet. mixed No
delay Minimum time in milliseconds before execution. integer Yes
param, … All following parameters will be passed to the callable. mixed Yes

Return values

Returns the id of the current interval.

This id can later be used with: clearTimeout.


Example #1 – setTimeout example

Logs out: First then Third then Second.

console.log('First'); setTimeout(function (param) { console.log('Second'); }, 200); console.log('Third');
Example #2 – Passing parameters

Passing one or more parameters to the function.

setTimeout(function (param1, param2) { console.log(param1, param2); }, 100, 'hello', 'world');
Example #3 – Recursive

Recursivly executes 2 seconds after last execution.

var timeout = function () { console.log('Executed.'); setTimeout(function () { timeout(); }, 2000); } timeout();

Same as above but output is moved to the end of the wait time.

var timeout = function () { setTimeout(function () { console.log('Executed.'); timeout(); }, 2000); } timeout();

See also

  • clearTimeout

External references