Console.countReset()

这篇翻译不完整。请帮忙从英语翻译这篇文章

Resets the counter. This function takes an optional argument label.

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

If label is supplied, this function resets the count associated with that particular label.

If label is omitted, the function resets the default counter.

语法

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 AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge ? Firefox Full support 62IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 62Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

文档标签和贡献者

此页面的贡献者: 807573515
最后编辑者: 807573515,