Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Warning: When the code parameter is used, this method dynamically executes its value as JavaScript. APIs like this are known as injection sinks, and are potentially a vector for cross-site-scripting (XSS) attacks.
You can mitigate this risk by always assigning TrustedScript objects instead of strings and enforcing trusted types. See Security considerations for more information.
The setTimeout() method of the Window interface sets a timer which executes a function or specified piece of code once the timer expires.
A function to be executed after the timer expires.
codeA TrustedScript or a string of arbitrary code that is compiled and executed every delay milliseconds. This can be used instead of passing a function, but is strongly discouraged for the same reasons that make using eval() a security risk.
delay OptionalA non-negative integer indicating how long the timer should wait before the specified function or code is executed, in milliseconds. Defaults to 0 if not specified.
Note:
Additional arguments which are passed through to the function specified by func.
A positive integer (typically within the range of 1 to 2,147,483,647) that uniquely identifies the timer created by the call. This identifier, often referred to as a "timeout ID", can be passed to clearTimeout() to cancel the timer.
Within the same global environment (e.g., a specific window or worker) the timeout ID is guaranteed not to be reused for any new timer as long as the original timer remains active. However, separate global environments maintain their own independent pools of timer IDs.
The code can't be parsed as a script.
TypeErrorThrown if the code parameter is set to a string when Trusted Types are enforced by a CSP and no default policy is defined. It is also thrown if the first parameter is not one of the supported types: a function, string or TrustedScript.
The setTimeout() function is commonly used to call a function that is executed just once, after a delay. You can call Window.clearTimeout() to cancel the timeout before it completes.
If you wish to call a function repeatedly (e.g., every N milliseconds), you can use setInterval().
setTimeout() is an asynchronous function, meaning that it returns immediately after scheduling the callback function or code to run. It does not "wait", blocking execution of the lines of code after setTimeout() until the scheduled code has run.
Consider the following example:
The setTimeout() method is called three times, passing a callback function that logs the order in which setTimeout() was called. Because the earlier methods calls have larger delays, the callback methods are executed in reverse order to which they were scheduled. If setTimeout() blocked until the callback completed, the output would display the messages in order.
Asynchronous methods are useful because they allow tasks to be run in parallel when the order of execution does not matter. If the order that an asynchronous method completes does matter, then you can use Promises (promise chaining) to wait on the completion of a task.
The functions passed to setTimeout() is run with normal function call semantics for determining the reference of this. This problem is explained in detail in the JavaScript reference.
For non-arrow functions, the this context is set to the globalThis (an alias for window in browsers) object.
The following example demonstrates how this can cause unexpected behavior. Here, when we pass the method counter.count directly to setTimeout(), the this context is lost, and the method is called on the global object instead of the Counter instance, resulting in a TypeError when the count method tries to access this:
To work around this, you must make sure that the function passed to setTimeout has the correct this context. There are three main ways to do this:
If you want to explicitly specify the this context, instead of passing the method directly, wrap the method call in another anonymous function that explicitly calls the method with the correct context:
If you want to use the this context of the code that calls setTimeout(), always use an arrow function, which inherits the this context of its enclosing scope:
If you want to avoid extra function wrappers (which increase memory usage) while explicitly specifying the this context, you can use the Function.prototype.bind() method to create a new function with the correct this context:
If setTimeout() is called with delay value that's not a number, implicit type coercion is silently done on the value to convert it to a number. For example, the following code incorrectly uses the string "1000" for the delay value, rather than the number 1000 – but it nevertheless works, because when the code runs, the string is coerced into the number 1000, and so the code executes 1 second later.
In many cases, the implicit type coercion can lead to unexpected and surprising results. For example, when the following code runs, the string "1 second" ultimately gets coerced into the number 0 — and so, the code executes with zero delay.
Therefore, don't use strings for the delay value but instead always use numbers:
The delay argument is converted to a signed 32-bit integer, which limits the value to 2147483647 ms, or roughly 24.8 days. Delays of more than this value will cause an integer overflow. So for example, this code:
…results in the timeout being executed immediately (since 2**32 - 5000 overflows to a negative number), while the following code:
…results in the timeout being executed after approximately 5 seconds.
Note: In Node.js, any timeout larger than 2,147,483,647 ms results in immediate execution.
There are a number of reasons why a timeout may take longer to fire than anticipated. This section describes the most common reasons.
As specified in the HTML standard, browsers will enforce a minimum timeout of 4 milliseconds once a nested call to setTimeout has been scheduled 5 times.
This can be seen in the following example, in which we nest a call to setTimeout with a delay of 0 milliseconds, and log the delay each time the handler is called. The first four times, the delay is approximately 0 milliseconds, and after that it is approximately 4 milliseconds:
To reduce the load (and associated battery usage) from background tabs, browsers will enforce a minimum timeout delay in inactive tabs. It may also be waived if a page is playing sound using a Web Audio API AudioContext.
The specifics of this are browser-dependent:
Firefox Desktop has a minimum timeout of 1 second for inactive tabs.
Firefox for Android has a minimum timeout of 15 minutes for inactive tabs and may unload them entirely.
Firefox does not throttle inactive tabs if the tab contains an AudioContext.
Chrome uses different levels of throttling depending on the tab activity:
Minimal throttling: Applies to timers when the page is visible, has made sound recently, or is otherwise considered active by Chrome. Timers run close to the requested interval.
Throttling: Applies to timers when minimal throttle conditions are not met and any of these conditions are true:
Timers in this state are checked once per second, which may be batched together with other timers that have similar timeouts.
Timers in this state are checked once per minute, which may be batched together with other timers that have similar timeouts.
Firefox enforces additional throttling for scripts that it recognizes as tracking scripts. When running in the foreground, the throttling minimum delay is still 4ms. In background tabs, however, the throttling minimum delay is 10,000 ms, or 10 seconds, which comes into effect 30 seconds after a document has first loaded.
See Tracking Protection for more details.
The timeout can also fire later than expected if the page (or the OS/browser) is busy with other tasks. One important case to note is that the function or code snippet cannot be executed until the thread that called setTimeout() has terminated. For example:
Will write to the console:
After setTimeout foo has been calledThis is because even though setTimeout was called with a delay of zero, it's placed on a queue and scheduled to run at the next opportunity; not immediately. Currently-executing code must complete before functions on the queue are executed, thus the resulting execution order may not be as expected.
Firefox will defer firing setTimeout() timers while the current tab is loading. Firing is deferred until the main thread is deemed idle (similar to Window.requestIdleCallback()), or until the load event is fired.
In WebExtensions, setTimeout() does not work reliably. Extension authors should use the alarms API instead.
The method can be used to execute arbitrary input passed in the code parameter. If the input is a potentially unsafe string provided by a user, this is a possible vector for Cross-site-scripting (XSS) attacks.
For example, the following code shows how setTimeout() might execute untrustedCode provided by a user:
Websites with a Content Security Policy (CSP) that specifies script-src or default-src will prevent such code running by default. You can specify unsafe-eval in your CSP to allow setTimeout() to execute, but this is unsafe as it disables one of the main protections of CSP. See Inline JavaScript in the CSP guide.
If you must allow the scripts to run via setTimeout() you can mitigate these issues by always assigning TrustedScript objects instead of strings, and enforcing trusted types using the require-trusted-types-for CSP directive. This ensures that the input is passed through a transformation function.
To allow setTimeout() to run, you will additionally need to specify the trusted-types-eval keyword in your CSP script-src directive. This acts in the same way as unsafe-eval, but only allows the method to evaluate if trusted types are enabled (if you were to use unsafe-eval it would allow execution even on browsers that do not support trusted types).
For example, the required CSP for your site might look like this:
The behavior of the transformation function will depend on the specific use case that requires a user provided script. If possible you should lock the allowed scripts to exactly the code that you trust to run. If that is not possible, you might allow or block the use of certain functions within the provided string.
Note that these examples omit the use of trusted types for brevity. See Using TrustedScript in eval() for code showing the expected approach.
The following example sets up two simple buttons in a web page and hooks them to the setTimeout() and clearTimeout() routines. Pressing the first button will set a timeout which shows a message after two seconds and stores the timeout id for use by clearTimeout(). You may optionally cancel this timeout by pressing on the second button.
See also the clearTimeout() example.
| HTML # dom-settimeout-dev |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Mar 30, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.