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

Window clearTimeout() method

Window clearTimeout() method

Window Object Reference Window Object

Example

Prevent the setTimeout() method from executing a function:

var

myVar

;

function

myFunction

(

)

{

myVar

=

setTimeout

(

function

(

)

{

alert

(

"

Hello

"

)

;

}

,

3000

)

;

}

function

myStopFunction

(

)

{

clearTimeout

(

myVar

)

;

}

Try it »


Definition and Usage

The clearTimeout() method cancels the timed operation set by setTimeout() the method.

The parameter of the clearTimeout() method must be the ID value returned by setTimeout().

Note: To use the clearTimeout() method, you must use a global variable when creating the timed operation:



myVar = setTimeout("javascript function", milliseconds);

If the method has not yet been executed, we can use clearTimeout() to prevent it.


Browser support

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

Method
clearTimeout() 1.0 4.0 1.0 1.0 4.0

Syntax



clearTimeout(id_of_settimeout)

Parameters Description
id_of_setinterval The return value obtained when calling the setTimeout() function can be used as a parameter to cancel the timed operation set by that setTimeout().

Technical Details

Return value: No value is returned.

More examples

Example

The example below starts a counting program when you click the "Start counting!" button. After stopping by clicking the "Stop counting!" button, you can click the "Start counting!" button again to restart counting.

<button onclick="startCount()">

Start counting!

button

<input type="text" id="txt">
<button onclick="stopCount()">

Stop counting!

button

<

script

var

c

=

0

;

var

t

;

var

timer_is_on

=

0

;

function

timedCount

(

)

{

document

.

getElementById

(

"

txt

"

)

.

value

=

c

;

c

=

c

1

;

t

=

setTimeout

(

function

(

)

{

timedCount

(

)

}

,

1000

)

;

}

function

startCount

(

)

{

if

(

!

timer_is_on

)

{

timer_is_on

=

1

;

timedCount

(

)

;

}

}

function

stopCount

(

)

{

clearTimeout

(

t

)

;

timer_is_on

=

0

;

}

script

Try it »


Related pages

Window object: setInterval() method

Window object: setTimeout() method

Window object: clearInterval() method

Window Object Reference Window Object