Console.countReset()

翻译不完整。 请帮助我们翻译这篇文章!

重置计数器。此函数有一个可选参数 label

Note: 此特性在 Web Worker 中可用。

如果提供了参数label,此函数会重置与label关联的计数。

如果省略了参数label,此函数会重置默认的计数器。

语法

console.countReset([label]);

参数

label
一个字符串, 若传入此参数 countReset() 重置此label的count为0。
若忽略此参数  countReset() 重置count()默认的 default 字段的count为0
 

返回值

若传入label参数:

 counter-name: 0

若不传入label参数:

default: 0

异常情况

若传入一个不存在的 labelcountReset 返回下面的警告信息:

Counter "counter-name" doesn’t exist.

若 label 没有被传入 并且 count() 也没有被调用过, countReset 返回下面的警告信息:

Counter "default" doesn’t exist.

示例

下面给出示例代码:

var user = "";

function greet() {
  console.count();
  return "hi " + user;
}

user = "bob";
greet();
user = "alice";
greet();
greet();
console.count();
console.countReset();

控制台打印输出结果:

"default: 1"
"default: 2"
"default: 3"
"default: 1"
"default: 0"

Note that the call to console.counterReset() resets the value of the default counter to zero.

可以看到 调用 console.counterReset() 重置了default 的计数为0

如果我们把 user 变量做为 label 传入第一次调用的 count()  把字符串 'alice' 作为第二次调用count() 的参数

var user = "";

function greet() {
  console.count(user);
  return "hi " + user;
}

user = "bob";
greet();
user = "alice";
greet();
greet();
console.countReset("bob");
console.count("alice");

我们看到的输出如下:

"bob: 1"
"alice: 1"
"alice: 2"
"bob: 0"
"alice: 3"

调用countReset("bod")只是重置了 "bob" 的计数器值  而 "alice" 的计数器值没有改变。

Specifications

Specification Status Comment
Console API
console.countReset()
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
countResetChrome Full support 68Edge Full support 79Firefox Full support 62IE No support NoOpera Full support 55Safari Full support 13WebView Android Full support 68Chrome Android Full support 68Firefox Android Full support 62Opera Android Full support 48Safari iOS Full support 13Samsung Internet Android Full support 10.0

Legend

Full support  
Full support
No support  
No support