未验证 提交 db578344 编写于 作者: O openharmony_ci 提交者: Gitee

!17736 Fix console/timer description

Merge pull request !17736 from yaochaonan/docs
# console (日志打印)
# console 控制台
本模块提供基础的日志打印能力,支持按照日志级别打印日志信息。
如果需要使用更高级的日志打印服务,比如按照指定标识筛选日志内容,推荐使用[`@ohos.hilog`](js-apis-hilog.md)
本模块提供了一个简单的调试控制台,类似于浏览器提供的JavaScript控制台机制。
> **说明:**
>
......@@ -10,9 +8,9 @@
## console.debug
debug(message: string): void
debug(message: string, ...arguments: any[]): void
打印debug级别的日志信息。
以格式化输出方式打印调试信息。
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
......@@ -21,13 +19,24 @@ debug(message: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| message | string | 是 | 表示要打印的文本信息。 |
| arguments | any | 否 | 表示其余要打印的信息或message的替换值。 |
**示例:**
```js
const number = 5;
console.debug('count: %d', number); // 格式化输出替换message中的文本。
// count: 5
console.debug('count:', number); // 打印message以及其余信息
// count: 5
console.debug('count:'); // 仅打印message
// count:
```
## console.log
log(message: string): void
log(message: string, ...arguments: any[]): void
打印debug级别的日志信息。
以格式化输出方式打印日志信息。
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
......@@ -36,13 +45,24 @@ log(message: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| message | string | 是 | 表示要打印的文本信息。 |
| arguments | any | 否 |表示其余要打印的信息或message的替换值。 |
**示例:**
```js
const number = 5;
console.log('count: %d', number); // 格式化输出替换message中的文本。
// count: 5
console.log('count:', number); // 打印message以及其余信息
// count: 5
console.log('count:'); // 仅打印message
// count:
```
## console.info
info(message: string): void
info(message: string, ...arguments: any[]): void
打印info级别的日志信息
以格式化输出方式打印日志信息。(console.log()的别名)
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
......@@ -51,13 +71,24 @@ info(message: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| message | string | 是 | 表示要打印的文本信息。 |
| arguments | any | 否 | 表示其余要打印的信息或message的替换值。 |
**示例:**
```js
const number = 5;
console.info('count: %d', number); // 格式化输出替换message中的文本。
// count: 5
console.info('count:', number); // 打印message以及其余信息
// count: 5
console.info('count:'); // 仅打印message
// count:
```
## console.warn
warn(message: string): void
warn(message: string, ...arguments: any[]): void
打印warn级别的日志信息。
以格式化输出方式打印警告信息。
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
......@@ -65,14 +96,25 @@ warn(message: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| message | string | 是 | 表示要打印的文本信息。 |
| message | string | 是 | 表示要打印的警告信息。 |
| arguments | any | 否 | 表示其余要打印的信息或message的替换值。 |
**示例:**
```js
const str = "name should be string";
console.warn('warn: %d', str); // 格式化输出替换message中的文本。
// warn: name should be string
console.warn('warn:', str); // 打印message以及其余信息
// warn: name should be string
console.warn('warn:'); // 仅打印message
// warn:
```
## console.error
error(message: string): void
error(message: string, ...arguments: any[]): void
打印error级别的日志信息。
以格式化输出方式打印错误信息。
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
......@@ -80,31 +122,26 @@ error(message: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| message | string | 是 | 表示要打印的文本信息。 |
| message | string | 是 | 表示要打印的错误信息。 |
| arguments | any | 否 | 表示其余要打印的信息或message的替换值。 |
**示例:**
```
export default {
clickConsole(){
var versionCode = 1;
console.info('Hello World. The current version code is ' + versionCode);
console.log(`versionCode: ${versionCode}`);
// 以下写法从API Version 6开始支持console.log('versionCode:%d.', versionCode);
}
}
```js
const str = "value is not defined";
console.error('error: %d', str); // 格式化输出替换message中的文本。
// error: value is not defined
console.error('error:', str); // 打印message以及其余信息
// error: value is not defined
console.error('error:'); // 仅打印message
// error:
```
在DevEco Studio的底部,切换到“HiLog”窗口。选择当前的设备及进程,日志级别选择Info,搜索内容设置为“Hello World”。此时窗口仅显示符合条件的日志,效果如图所示:
![zh-cn_image_0000001200913929](figures/zh-cn_image_0000001200913929.png)
## console.assert<sup>10+</sup>
assert(value?: Object, ...arguments: Object[]): void
若value为假,打印后续内容
断言打印
**系统能力:** SystemCapability.Utils.Lang
......@@ -112,24 +149,26 @@ assert(value?: Object, ...arguments: Object[]): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| value | Object | 否 | |
| arguments | Object | 否 | 错误消息打印。 |
| value | Object | 否 | 语句结果值。若value为假(false)或者省略,则输出以"Assertion failed"开头。如果 value 为真值(true),则无打印。|
| arguments | Object | 否 | value为假(false)的后续错误消息打印。省略则不打印。|
**示例:**
```
console.assert(true, 'does nothing');
```js
console.assert(true, 'does nothing'); // 表达式结果值为true, 无打印。
console.assert(2 % 1 == 0, 'does nothing'); // 表达式结果值为true, 无打印。
console.assert(false, 'console %s work', 'didn\'t');
// Assertion console:ohos didn't work
// Assertion failed: console didn't work
console.assert();
// Assertion failed
```
## console.count<sup>10+</sup>
count(label?: string): void
维护一个内部计数器, 并输出调用label的console.count()次数。默认值为'default'
维护一个内部计数器,调用时,打印此标签名以及对应的计数次数
**系统能力:** SystemCapability.Utils.Lang
......@@ -137,10 +176,11 @@ count(label?: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| label | string | 否 | 计数器标签名。 |
| label | string | 否 | 计数器标签名。默认值为'default'。|
**示例:**
```
```js
console.count()
// default: 1
console.count('default')
......@@ -150,7 +190,7 @@ console.count('abc')
console.count('xyz')
// xyz: 1
console.count('abc')
abc: 2
// abc: 2
console.count()
// default: 3
```
......@@ -159,7 +199,7 @@ console.count()
countReset(label?: string): void
清除label名的计数。默认值为'default'
清除指定标签名的计数
**系统能力:** SystemCapability.Utils.Lang
......@@ -167,10 +207,10 @@ countReset(label?: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| label | string | 否 | 计数器标签名。 |
| label | string | 否 | 计数器标签名。默认值为'default'。|
**示例:**
```
```js
console.count('abc');
// abc: 1
console.countReset('abc');
......@@ -190,13 +230,24 @@ dir(dir?: Object): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| dir | Object | 否 | 需要打印内容的对象。 |
| dir | Object | 否 | 需要打印内容的对象。省略则无任何打印。|
**示例:**
```js
let a = { foo: { bar: { baz: true } }};
console.dir(a);
// Object: {"foo":{"bar":{"baz":true}}}
console.dir(); // 无打印
```
## console.dirxml<sup>10+</sup>
dirxml(...arguments: Object[]): void
此方法调用 console.log() 将接收到的参数传给它。此方法不会产生任何 XML 格式
此方法通过内部调用console.log()实现。此方法不会产生任何 XML 格式。使用方法与console.log()一致
**系统能力:** SystemCapability.Utils.Lang
......@@ -204,14 +255,25 @@ dirxml(...arguments: Object[]): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| arguments | Object | 否 | 要打印的信息。 |
| arguments | Object | 是 | 要打印的信息。 |
**示例:**
```js
const number = 5;
console.dirxml('count: %d', number);
// count: 5
console.dirxml('count:', number);
// count: 5
console.dirxml('count:');
// count:
```
## console.group<sup>10+</sup>
group(...arguments: Object[]): void
将后续行的缩进增加 groupIndentation 长度的空格。
如果提供需要打印的信息,首先先打印信息,没有额外的缩进。
默认将后续行的缩进增加两个空格。
如果提供需要打印的信息,则首先打印信息,没有额外的缩进。
**系统能力:** SystemCapability.Utils.Lang
......@@ -220,11 +282,26 @@ group(...arguments: Object[]): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| arguments | Object | 否 | 要打印的信息。 |
**示例:**
```js
console.log("outter");
// outter
console.group();
console.log("level 1");
// level 1
console.group("in level1");
// in level1
console.log("level 2");
// level 2
```
## console.groupCollapsed<sup>10+</sup>
groupCollapsed(...arguments: Object[]): void
group的别名
使用与功能同console.group()一致
**系统能力:** SystemCapability.Utils.Lang
......@@ -234,14 +311,42 @@ group的别名。
| ------- | ------ | ---- | ----------- |
| arguments | Object | 否 | 要打印的信息。|
**示例:**
```js
console.groupCollapsed("outter");
// outter
console.groupCollapsed();
console.log("level 1");
// level 1
console.groupCollapsed("in level1");
// in level1
console.log("level 2");
// level 2
```
## console.groupEnd<sup>10+</sup>
groupEnd(): void
将后续行的缩进减少 groupIndentation 长度的空格。
将后续行的缩进减少两个空格。
**系统能力:** SystemCapability.Utils.Lang
**示例:**
```js
console.log("outter");
// outter
console.group();
console.log("level 1");
// level 1
console.groupEnd();
console.log("outter");
// outter
```
## console.table<sup>10+</sup>
table(tableData?: Object): void
......@@ -254,10 +359,10 @@ table(tableData?: Object): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| tableData | Object | 否 | 要打印为表格形式的对象。 |
| tableData | Object | 否 | 要打印为表格形式的对象。省略则无任何打印。 |
**示例:**
```
```js
console.table([1, 2, 3]);
// ┌─────────┬────────┐
// │ (index) │ Values │
......@@ -281,7 +386,7 @@ console.table({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } });
time(label?: string): void
启动可用于计算操作持续时间的计时器。默认值为'default'。可使用console.timeEnd()关闭计时器并打印结果
启动可用于计算操作持续时间的计时器。可使用console.timeEnd()关闭计时器并打印经过的时间(单位:ms)
**系统能力:** SystemCapability.Utils.Lang
......@@ -289,13 +394,18 @@ time(label?: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| label | string | 否 | 计时器标识。 |
| label | string | 否 | 计时器标识。默认值为'default'。 |
**示例:**
```js
console.time('abc');
```
## console.timeEnd<sup>10+</sup>
timeEnd(label?: string): void
停止之前通过调用 console.time() 启动的计时器并将结果打印。默认值为'default'
停止之前通过调用 console.time() 启动的计时器并将打印经过的时间(单位:ms)
**系统能力:** SystemCapability.Utils.Lang
......@@ -303,10 +413,10 @@ timeEnd(label?: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| label | string | 否 | 计时器标识。 |
| label | string | 否 | 计时器标识。默认值为'default' |
**示例:**
```
```js
console.time('abc');
console.timeEnd('abc');
// abc: 225.438ms
......@@ -316,7 +426,7 @@ console.timeEnd('abc');
timeLog(label?: string, ...arguments: Object[]): void
对于先前通过调用 console.time() 启动的计时器,打印经过时间和其他 data 参数。
对于先前通过调用 console.time() 启动的计时器,打印经过时间和其他data参数。
**系统能力:** SystemCapability.Utils.Lang
......@@ -324,14 +434,13 @@ timeLog(label?: string, ...arguments: Object[]): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| label | string | 否 | 计时器标识。 |
| label | string | 否 | 计时器标识。默认值为'default' |
| arguments | Object | 否 | 需要打印的其他日志。 |
**示例:**
```
```js
console.time('timer1');
const value = aaa(); // 返回 17
console.timeLog('timer1', value);
console.timeLog('timer1', 17);
// timer1: 365.227ms 17
console.timeEnd('timer1');
// timer1: 513.22ms
......@@ -349,10 +458,14 @@ trace(...arguments: Object[]): void
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ----------- |
| arguments | Object | 否 | 需要打印的其他日志。 |
| arguments | Object | 否 | 需要打印的其他日志。省略则仅打印堆栈信息。|
**示例:**
```
```js
console.trace();
// Trace:
// xxxxxxxxxx(当前堆栈信息)
console.trace("Show the trace");
// Trace: Show the trace
// xxxxxxxxxx(当前堆栈信息)
```
\ No newline at end of file
......@@ -18,7 +18,7 @@ setTimeout(handler: Function | string, delay?: number, ...arguments: any[]): num
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| handler | Function \| string | 是 | 定时器到期后执行函数。 |
| handler | Function \| string | 是 | 定时器到期后执行函数。类型为string则打印Error信息,不进行其他处理。 |
| delay | number | 否 | 延迟的毫秒数,函数的调用会在该延迟之后发生。如果省略该参数,delay取默认值0,意味着“马上”执行,或尽快执行。 |
| ...arguments | Array&lt;any&gt; | 否 | 附加参数,一旦定时器到期,它们会作为参数传递给handler。 |
......@@ -26,24 +26,20 @@ setTimeout(handler: Function | string, delay?: number, ...arguments: any[]): num
| 类型 | 说明 |
| -------- | -------- |
| number | timeout定时器的ID。 |
| number | 定时器的ID。 |
**示例:**
```js
export default {
setTimeOut() {
var timeoutID = setTimeout(function() {
console.log('delay 1s');
}, 1000);
}
}
setTimeout(function() {
console.log('delay 1s');
}, 1000);
```
## clearTimeout
clearTimeout(timeoutID: number): void
clearTimeout(timeoutID?: number): void
取消了先前通过调用setTimeout()建立的定时器。
......@@ -53,19 +49,15 @@ clearTimeout(timeoutID: number): void
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| timeoutID | number | 是 | 要取消定时器的ID,&nbsp;是由setTimeout()返回的。 |
| timeoutID | number | 否 | 要取消定时器的ID,&nbsp;是由setTimeout()返回的。如果省略该参数,则不取消任何定时任务,无任何处理。|
**示例:**
```js
export default {
clearTimeOut() {
var timeoutID = setTimeout(function() {
console.log('do after 1s delay.');
}, 1000);
clearTimeout(timeoutID);
}
}
```js
let timeoutID = setTimeout(function() {
console.log('do after 1s delay.');
}, 1000);
clearTimeout(timeoutID);
```
......@@ -81,32 +73,28 @@ setInterval(handler: Function | string, delay: number, ...arguments: any[]): num
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| handler | Function \| string | 是 | 要重复调用的函数。 |
| delay | number | 是 | 延迟的毫秒数(一秒等于1000毫秒),函数的调用会在该延迟之后发生。 |
| handler | Function \| string | 是 | 要重复调用的函数。类型为string则打印Error信息,不进行其他处理。|
| delay | number | 是 | 延迟的毫秒数,函数的调用会在该延迟之后发生。 |
| ...arguments | Array&lt;any&gt; | 否 | 附加参数,一旦定时器到期,他们会作为参数传递给handler。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | intervalID重复定时器的ID。 |
| number | 定时器的ID。 |
**示例:**
```js
export default {
setInterval() {
var intervalID = setInterval(function() {
console.log('do very 1s.');
}, 1000);
}
}
setInterval(function() {
console.log('do every 1s.');
}, 1000);
```
## clearInterval
clearInterval(intervalID: number): void
clearInterval(intervalID?: number): void
可取消先前通过setInterval()设置的重复定时任务。
......@@ -116,17 +104,13 @@ clearInterval(intervalID: number): void
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| intervalID | number | 是 | 要取消的重复定时器的ID,是由&nbsp;setInterval()&nbsp;返回的。 |
| intervalID | number | 否 | 要取消的重复定时器的ID,是由&nbsp;setInterval()&nbsp;返回的。如果省略该参数,则不取消任何定时任务,无任何处理。|
**示例:**
```js
export default {
clearInterval() {
var intervalID = setInterval(function() {
console.log('do very 1s.');
}, 1000);
clearInterval(intervalID);
}
}
```js
let intervalID = setInterval(function() {
console.log('do every 1s.');
}, 1000);
clearInterval(intervalID);
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册