Home

setTimeout

setTimeout is a timer function available in web browsers and Node.js that schedules the execution of a function after a specified delay. When invoked, it places the callback into the event loop's task queue to be run once the delay elapses. The call is asynchronous relative to the code that set it, allowing the rest of the program to continue executing.

Syntax: setTimeout(callback, delay, [arg1, arg2, ...]). Callback is invoked after delay milliseconds, optionally with the provided arguments.

In browsers, timer precision is not guaranteed. Timers may be clamped or throttled in inactive tabs or

Usage includes delaying actions, debouncing user input, and scheduling work to occur later in the event loop.

The
function
returns
a
timer
identifier,
which
can
be
used
with
clearTimeout
to
cancel
the
scheduled
callback
before
it
fires.
Delay
is
measured
in
milliseconds;
a
delay
of
0
yields
execution
as
soon
as
the
current
call
stack
is
cleared
in
most
environments.
complex
environments,
causing
delays
longer
than
requested.
For
0
or
small
delays,
the
timer
may
still
be
deferred
to
the
next
event
loop
cycle.
setTimeout
is
single-shot;
to
execute
repeatedly
at
a
fixed
rate,
use
setInterval
or
recursively
schedule
subsequent
timeouts.
It
is
distinct
from
setInterval,
which
repeats
until
cleared.
ClearTimeout
cancels
a
pending
timeout.
In
Node.js,
setTimeout
is
part
of
the
timers
API
and
behaves
similarly
to
its
browser
counterpart
for
scheduling
a
callback.