By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

JavaScript setTimeout Method

Reference on the setTimeout Method of JavaScript. How to use setTimeout, and useful examples.

Edited: 2012-08-23 12:46

The JavaScript setTimeout Method is used to do something after a specified number of milliseconds. A common use is to call a user made function

The setTimeout method can be used in animation, an example of this can be seen in Brugbarts JavaScript Fade Effect, where setTimeout was used to change the relevant CSS properties to make a fade effect.


The setTimeout Method takes three values: code, milliseconds, and lang. The last parameter is the optional lang parameter, which is used to tell what scripting language is used.

CodeThe code to be executed.
MillisecondsThe amount of time to wait in milliseconds.
Lang optionalThe language used. I.e. JavaScript, VBScript

JavaScript setTimeout Examples

The below example will wait 2 seconds when the botton is pressed, then create an alert box with the text "Hallo!".

<button onclick="setTimeout('window.alert(\'Hello!\')', 2000)">Click Me!</button>