| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/setTimeout | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is only available in Web Workers.
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 in Window.setTimeout() for more information.
The setTimeout() method of the WorkerGlobalScope interface sets a timer which executes a function or specified piece of code once the timer expires.
setTimeout(code)
setTimeout(code, delay)
setTimeout(func)
setTimeout(func, delay)
setTimeout(func, delay, param1)
setTimeout(func, delay, param1, param2)
setTimeout(func, delay, param1, param2, /* , */ paramN)
funcA 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 OptionalThe time that the timer should wait before the specified function or code is executed, in milliseconds. Defaults to 0 if not specified.
Note:
delay to 0 will execute in the next event cycle rather than "immediately".
See Reasons for longer delays than specified for more information.param1, , paramN OptionalAdditional 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.
SyntaxErrorThe 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.
See Window.setTimeout() for detailed descriptions.
See Window.setTimeout() for examples.
| Specification |
|---|
| HTML # dom-settimeout-dev |
setTimeout which allows passing arguments to the callback in core-jsWindow.setTimeout()WorkerGlobalScope.clearTimeout()WorkerGlobalScope.setInterval()DedicatedWorkerGlobalScope.requestAnimationFrame()WorkerGlobalScope.queueMicrotask()This page was last modified on Aug 11, 2026 by MDN contributors.
WorkerGlobalScopeBeforeUnloadEventDOMStringMapErrorEventHTMLAnchorElementHTMLAreaElementHTMLAudioElementHTMLBRElementHTMLBaseElementHTMLBodyElementHTMLButtonElementHTMLCanvasElementHTMLDListElementHTMLDataElementHTMLDataListElementHTMLDialogElementHTMLDivElementHTMLDocumentHTMLElementHTMLEmbedElementHTMLFieldSetElementHTMLFormControlsCollectionHTMLFormElementHTMLFrameSetElementHTMLGeolocationElementHTMLHRElementHTMLHeadElementHTMLHeadingElementHTMLHtmlElementHTMLIFrameElementHTMLImageElementHTMLInputElementHTMLLIElementHTMLLabelElementHTMLLegendElementHTMLLinkElementHTMLMapElementHTMLMediaElementHTMLMenuElementHTMLMetaElementHTMLMeterElementHTMLModElementHTMLOListElementHTMLObjectElementHTMLOptGroupElementHTMLOptionElementHTMLOptionsCollectionHTMLOutputElementHTMLParagraphElementHTMLPictureElementHTMLPreElementHTMLProgressElementHTMLQuoteElementHTMLScriptElementHTMLSelectElementHTMLSourceElementHTMLSpanElementHTMLStyleElementHTMLTableCaptionElementHTMLTableCellElementHTMLTableColElementHTMLTableElementHTMLTableRowElementHTMLTableSectionElementHTMLTemplateElementHTMLTextAreaElementHTMLTimeElementHTMLTitleElementHTMLTrackElementHTMLUListElementHTMLUnknownElementHTMLVideoElementHashChangeEventHistoryImageDataLocationMessageChannelMessageEventMessagePortNavigatorPageRevealEventPageSwapEventPageTransitionEventPluginPluginArrayPromiseRejectionEventRadioNodeListTimeRangesUserActivationValidityStateWindowWorkletGlobalScopeYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |