1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
---
title: WindowOrWorkerGlobalScope.clearTimeout()
slug: Web/API/clearTimeout
tags:
- API
- WindowOrWorkerGlobalScope
- clearTimeout
translation_of: Web/API/WindowOrWorkerGlobalScope/clearTimeout
original_slug: Web/API/WindowOrWorkerGlobalScope/clearTimeout
---
<div>
<div>{{APIRef("HTML DOM")}}</div>
</div>
<p>{{domxref("WindowOrWorkerGlobalScope")}}内置的<strong><code>clearTimeout()</code></strong>方法取消了先前通过调用{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}建立的定时器。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="notranslate"><em>scope</em>.clearTimeout(<em>timeoutID</em>)</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>timeoutID</code></dt>
<dd>您要取消定时器的标识符。 该ID由相应的<code>setTimeout()</code>调用返回。</dd>
</dl>
<p>值得注意的是,{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}和{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}使用共享的ID池, 意味着在技术上可以混用<code>clearTimeout()</code>和{{domxref("WindowOrWorkerGlobalScope.clearInterval", "clearInterval()")}} 。 但是,为了清楚起见,你应该避免这样做。</p>
<h2 id="Example" name="Example">示例</h2>
<p>在一个网页中运行如下脚本,并且点击一次页面。一秒钟后你会看见弹出一条信息。如果你在一秒内不停点击页面,弹出框将不再出现。</p>
<pre class="brush: js notranslate">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>
<h2 id="Notes" name="Notes">注意</h2>
<p>传入一个错误的 ID 给 <code>clearTimeout()</code>不会有任何影响;也不会抛出异常。</p>
<h2 id="Specification" name="Specification">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'WindowOrWorkerGlobalScope.clearTimeout()')}}</td>
<td>{{Spec2("HTML WHATWG")}}</td>
<td>Method moved to the <code>WindowOrWorkerGlobalScope</code> mixin in the latest spec.</td>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'clearTimeout()')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
{{Compat("api.clearTimeout")}}
<h2 id="See_also" name="See_also">更多</h2>
<ul>
<li>{{domxref("WindowTimers.setTimeout()")}}</li>
<li>{{domxref("WindowTimers.setInterval()")}}</li>
<li>{{domxref("WindowTimers.clearInterval()")}}</li>
<li>{{domxref("Window.requestAnimationFrame()")}}</li>
<li><a href="/en-US/docs/JavaScript/Timers/Daemons" title="JavaScript/Timers/Daemons"><em>Daemons</em> management</a></li>
</ul>
|