Window clearTimeout()Method

Window 对象参考手册Window Object

Example

Prevent the setTimeout() method from executing the function:

var myVar; function myFunction() { myVar = setTimeout(function(){ alert("Hello"); }, 3000); } function myStopFunction() { clearTimeout(myVar); }

Try it yourself »

Definition and Usage

The clearTimeout() method can cancel the timed operation set by thesetTimeout()setTimeout() 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 specify the first browser version that fully supports the property.

Method
clearTimeout() 1.0 4.0 1.0 1.0 4.0

Syntax

clearTimeout(id_of_settimeout)
Parameter Description
id_of_setinterval The return value obtained when calling the setTimeout() function. Using this returned identifier as a parameter can cancel the timed execution operation set by that setTimeout().

Technical Details

Return value: No return value.

More Examples

Example

The following example starts the counting program by clicking the "Start count!" button. After clicking the "Stop count!" button to stop it, you can click the "Start count!" button again to restart counting.

<button onclick="startCount()">Start count!</button> <input type="text" id="txt"> <button onclick="stopCount()">Stop count!</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 yourself »


Related Pages

Window Object:setInterval() Method

Window Object:setTimeout() Method

Window Object:clearInterval() Method

Window 对象参考手册Window Object

Other Extensions