Window.clearTimeout

  • Revision slug: DOM/window.clearTimeout
  • Revision title: window.clearTimeout
  • Revision id: 304465
  • Created:
  • Creator: jyasskin
  • Is current revision? No
  • Comment clearTimeout is included in HTML5, contrary to the previous text.

Revision Content

{{ DomRef() }}

Summary

Clears the delay set by window.setTimeout().

Syntax

window.clearTimeout(timeoutID)

where timeoutID is the ID of the timeout you wish to clear, as returned by window.setTimeout().

Example

Run the script below in the context of a web page and click on the page once. You'll see a message popping up in a second. If you keep clicking on the page once in a second, the alert never appears.

var alarm = {
  remind: function(aMessage) {
    alert(aMessage);
    delete this.timeoutID;
  },

  setup: function() {
    this.cancel();
    var self = this;
    this.timeoutID = window.setTimeout(function(msg) {self.remind(msg);}, 1000, "Wake up!");
  },

  cancel: function() {
    if(typeof this.timeoutID == "number") {
      window.clearTimeout(this.timeoutID);
      delete this.timeoutID;
    }
  }
};
window.onclick = function() { alarm.setup() };

Notes

Passing an invalid ID to clearTimeout does not have any effect (and doesn't throw an exception).

Specification

DOM Level 0. Specified in HTML5.

{{ languages( { "en": "en/DOM/window.clearTimeout", "fr": "fr/DOM/window.clearTimeout", "ja": "ja/DOM/window.clearTimeout", "pl": "pl/DOM/window.clearTimeout" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Clears the delay set by <a href="/en/DOM/window.setTimeout" title="en/DOM/window.setTimeout">window.setTimeout()</a>.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval">
window.clearTimeout(<em>timeoutID</em>)
</pre>
<p>where <code>timeoutID</code> is the ID of the timeout you wish to clear, as returned by <a href="/en/DOM/window.setTimeout" title="en/DOM/window.setTimeout">window.setTimeout()</a>.</p>
<h3 id="Example" name="Example">Example</h3>
<p>Run the script below in the context of a web page and click on the page once. You'll see a message popping up in a second. If you keep clicking on the page once in a second, the alert never appears.</p>
<pre class="brush: js">
var alarm = {
  remind: function(aMessage) {
    alert(aMessage);
    delete this.timeoutID;
  },

  setup: function() {
    this.cancel();
    var self = this;
    this.timeoutID = window.setTimeout(function(msg) {self.remind(msg);}, 1000, "Wake up!");
  },

  cancel: function() {
    if(typeof this.timeoutID == "number") {
      window.clearTimeout(this.timeoutID);
      delete this.timeoutID;
    }
  }
};
window.onclick = function() { alarm.setup() };</pre>
<h3 id="Notes" name="Notes">Notes</h3>
<p>Passing an invalid ID to <code>clearTimeout</code> does not have any effect (and doesn't throw an exception).</p>
<h3 id="Specification" name="Specification">Specification</h3>
<p><span style="line-height: 21px;">DOM Level 0.</span>&nbsp;Specified in <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-cleartimeout" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-cleartimeout">HTML5</a>.</p>
<p>{{ languages( { "en": "en/DOM/window.clearTimeout", "fr": "fr/DOM/window.clearTimeout", "ja": "ja/DOM/window.clearTimeout", "pl": "pl/DOM/window.clearTimeout" } ) }}</p>
Revert to this revision