Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Window setTimeout() Method

Window setTimeout() Method

Window Object Reference Window Object

Example

Pop up "Hello" after 3 seconds (3000 milliseconds):

setTimeout

(

function

(

)

{

alert

(

"

Hello

"

)

;

}

,

3000

)

;

Try it »


Definition and Usage

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

Tip: 1000 milliseconds = 1 second.

Tip: If you only want to repeat the execution, you can use setInterval() method.

Tip: Use clearTimeout() the method to prevent the function from executing.


Browser support

The numbers in the table indicate the first browser version that supports this property.

Method
setTimeout() 1.0 4.0 1.0 1.0 4.0

Syntax



setTimeout(code, milliseconds, param1, param2, ...)

setTimeout(function, milliseconds, param1, param2, ...)

Parameters Description
code/function Required. The code string to be called, or a function.
milliseconds Optional. The time, in milliseconds, to wait before executing or calling the code/function. Default is 0.
param1, param2, ... Optional. Additional arguments passed to the executed function (not supported by IE9 and earlier versions).

Technical Details

Return value: Returns an ID (a number) that can be passed to clearTimeout() to cancel the execution.

More examples

Example

Pop up "Hello" after 3 seconds (3000 milliseconds):

var

myVar

;

function

myFunction

(

)

{

myVar

=

setTimeout

(

alertFunc

,

3000

)

;

}

function

alertFunc

(

)

{

alert

(

"

Hello!

"

)

;

}

Try it »

Example

Modify the text in the input box at the 2nd, 4th, and 6th seconds:

var

x

=

document

.

getElementById

(

"

txt

"

)

;

setTimeout

(

function

(

)

{

x

.

value

=

"

2 seconds

"

}

,

2000

)

;

setTimeout

(

function

(

)

{

x

.

value

=

"

4 seconds

"

}

,

4000

)

;

setTimeout

(

function

(

)

{

x

.

value

=

"

6 seconds

"

}

,

6000

)

;

Try it »

Example

Open a new window and close it after 3 seconds:

var

myWindow

=

window

.

open

(

"

"

,

"

"

,

"

width=200, height=100

"

)

;

myWindow

.

document

.

write

(

"

This is a new window

"

)

;

setTimeout

(

function

(

)

{

myWindow

.

close

(

)

}

,

3000

)

;

Try it »

Example

Use clearTimeout() to prevent the function from executing:

var

myVar

;

function

myFunction

(

)

{

myVar

=

setTimeout

(

function

(

)

{

alert

(

"

Hello

"

)

}

,

3000

)

;

}

function

myStopFunction

(

)

{

clearTimeout

(

myVar

)

;

}

Try it »

Example

Counter -- can be stopped by clicking the button:

function

startCount

(

)

function

stopCount

(

)

Try it »

Example

Display the current time:

function

startTime

(

)

{

var

today

=

new

Date

(

)

;

var

h

=

today

.

getHours

(

)

;

var

m

=

today

.

getMinutes

(

)

;

var

s

=

today

.

getSeconds

(

)

;

//

For numbers

m

=

checkTime

(

m

)

;

s

=

checkTime

(

s

)

;

document

.

getElementById

(

"

txt

"

)

.

innerHTML

=

h

"

:

"

m

"

:

"

s

;

t

=

setTimeout

(

function

(

)

{

startTime

(

)

}

,

500

)

;

}

function

checkTime

(

i

)

{

if

(

i

<

10

)

{

i

=

"

0

"

i

;

}

return

i

;

}

Try it »

Example

Pass arguments to the alertFunc function (not supported by IE9 and earlier versions):

var

myVar

;

function

myStartFunction

(

)

{

myVar

=

setTimeout

(

alertFunc

,

2000

,

"

Runoob

"

,

"

Google

"

)

;

}

Try it »

However, if an anonymous function is used, all browsers support it:

var

myVar

;

function

myStartFunction

(

)

{

myVar

=

setTimeout

(

function

(

)

{

alertFunc

(

"

Runoob

"

,

"

Google

"

)

;

}

,

2000

)

;

}

Try it »


Related pages

Window object: setInterval() method

Window object: setTimeout() method

Window object: clearTimeout() method

Window Object Reference Window Object