Skip to main content
Version: 3.13.0

setTimeout()

The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

Syntax

setTimeout(code)
setTimeout(code, delay)

setTimeout(functionRef)
setTimeout(functionRef, delay)
setTimeout(functionRef, delay, param1)
setTimeout(functionRef, delay, param1, param2)
setTimeout(functionRef, delay, param1, param2, /* … ,*/ paramN)

Parameters

  • functionRef

    • : A function to be executed after the timer expires.
  • code

    • : An alternative syntax that allows you to include a string instead of a function, which is compiled and executed when the timer expires. This syntax is not recommended for the same reasons that make using eval() a security risk.
  • delay optional

    • : The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
  • param1, …, paramN optional

    • : Additional arguments which are passed through to the function specified by functionRef.

Return value

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(). This value can be passed to clearTimeout() to cancel the timeout.

It is guaranteed that a timeoutID value will never be reused by a subsequent call to setTimeout() or setInterval() on the same object (a window or a worker). However, different objects use separate pools of IDs.

Description

Timeouts are cancelled using clearTimeout().

To call a function repeatedly (e.g., every N milliseconds), consider using setInterval().