blob: 237fbef76224e9c32b5dd792c4456301b71a0370 (
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
|
---
title: Console.countReset()
slug: Web/API/Console/countReset
translation_of: Web/API/Console/countReset
---
<div>{{APIRef("Console API")}}</div>
<p>重置计数器。此函数有一个可选参数 <code>label</code>。</p>
<p>{{AvailableInWorkers}}</p>
<p>如果提供了参数<code>label</code>,此函数会重置与label关联的计数。</p>
<p>如果省略了参数<code>label</code>,此函数会重置默认的计数器。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">console.countReset([label]);
</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>label</code></dt>
<dd>一个字符串, 若传入此参数 <code>countReset() </code>重置此label的count为0。</dd>
<dd>若忽略此参数 <code>countReset()</code> 重置count()默认的 default 字段的count为0<br>
</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>若传入label参数:</p>
<pre> <span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body">counter-name: 0</span></span></span></pre>
<p>若不传入label参数:</p>
<pre><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body">default: 0</span></span></span></pre>
<h3 id="异常情况">异常情况</h3>
<p>若传入一个不存在的 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);"><code>label</code></span></font>, <span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><code>countReset</code> 返回下面的警告信息:</span></span></span></p>
<pre><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><code>Counter "counter-name" doesn’t exist.</code></span></span></span></pre>
<p>若 <code>label</code> 没有被传入 并且 <code>count()</code> 也没有被调用过, <code>countReset</code> 返回下面的警告信息:</p>
<pre><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><code>Counter "default" doesn’t exist.</code></span></span></span></pre>
<h2 id="示例">示例</h2>
<p>下面给出示例代码:</p>
<pre class="brush: js">var user = "";
function greet() {
console.count();
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.count();
console.countReset();</pre>
<p>控制台打印输出结果:</p>
<pre class="eval">"default: 1"
"default: 2"
"default: 3"
"default: 1"
"default: 0"
</pre>
<p>Note that the call to console.counterReset() resets the value of the default counter to zero.</p>
<p>可以看到 调用 <code>console.counterReset()</code> 重置了<code>default</code> 的计数为0</p>
<p>如果我们把 <code>user</code> 变量做为 <code>label</code> 传入第一次调用的 <code>count() </code> 把字符串 <code>'alice' </code>作为第二次调用<code>count()</code> 的参数</p>
<pre class="brush: js">var user = "";
function greet() {
console.count(user);
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.countReset("bob");
console.count("alice");</pre>
<p>我们看到的输出如下:</p>
<pre class="eval">"bob: 1"
"alice: 1"
"alice: 2"
"bob: 0"
"alice: 3"</pre>
<p>调用countReset("bod")只是重置了 "bob" 的计数器值 而 "alice" 的计数器值没有改变。</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("Console API", "#count", "console.countReset()")}}</td>
<td>{{Spec2("Console API")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Console.countReset")}}</p>
|