aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html
blob: 4e700ea7d07d72595d236c9a124ccf1627b52d92 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
title: WindowOrWorkerGlobalScope.clearTimeout()
slug: Web/API/WindowOrWorkerGlobalScope/clearTimeout
tags:
  - API
  - WindowOrWorkerGlobalScope
  - clearTimeout
translation_of: Web/API/WindowOrWorkerGlobalScope/clearTimeout
original_slug: Web/API/WindowTimers/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>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1")}}<br>
    {{CompatGeckoDesktop("52")}}<sup>[1]</sup></td>
   <td>4.0</td>
   <td>4.0</td>
   <td>1.0</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>1.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1")}}<br>
    {{CompatGeckoMobile("52")}}<sup>[1]</sup></td>
   <td>6.0</td>
   <td>6.0</td>
   <td>1.0</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>clearTimeout()</code> now defined on {{domxref("WindowOrWorkerGlobalScope")}} mixin.</p>

<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>