Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web/api/performanceobserver h2m Performance observer #7280

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions files/zh-cn/web/api/performanceobserver/disconnect/index.html

This file was deleted.

43 changes: 43 additions & 0 deletions files/zh-cn/web/api/performanceobserver/disconnect/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: PeformanceObserver.disconnect()
slug: Web/API/PerformanceObserver/disconnect
translation_of: Web/API/PerformanceObserver/disconnect
---
{{APIRef("Performance Timeline API")}}

{{domxref('PerformanceObserver')}} 接口的 **`disconnect()`** 方法用于阻止性能观察者接收任何 {{domxref("PerformanceEntry","性能条目", '', 'true')}} 事件。

## 语法

```js
performanceObserver.disconnect();
```

## 例子

```js
var observer = new PerformanceObserver(function(list, obj) {
var entries = list.getEntries();
for (var i=0; i < entries.length; i++) {
// Process "mark" and "frame" events
}
});
observer.observe({entryTypes: ["mark", "frame"]});

function perf_observer(list, observer) {
// Process the "measure" event
// ...
// Disable additional performance events
observer.disconnect();
}
var observer2 = new PerformanceObserver(perf_observer);
observer2.observe({entryTypes: ["measure"]});
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
57 changes: 0 additions & 57 deletions files/zh-cn/web/api/performanceobserver/index.html

This file was deleted.

46 changes: 46 additions & 0 deletions files/zh-cn/web/api/performanceobserver/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: 性能监测对象
slug: Web/API/PerformanceObserver
tags:
- API
- 性能
- 性能记录
- 接口
translation_of: Web/API/PerformanceObserver
---
{{APIRef("Performance Timeline API")}}

**`PerformanceObserver`** 用于监测性能度量事件,在浏览器的性能时间轴记录下一个新的 {{domxref("PerformanceEntry","performance entries", '', 'true')}} 的时候将会被通知 。

{{AvailableInWorkers}}

## 构造函数

- {{domxref("PerformanceObserver.PerformanceObserver","PerformanceObserver()")}}
- : 创建并返回一个新的 `PerformanceObserver` 对象。

## 方法

- {{domxref("PerformanceObserver.observe","PerformanceObserver.observe()")}}
- : 指定监测的 {{domxref("PerformanceEntry.entryType","entry types")}} 的集合。 当 {{domxref("PerformanceEntry","performance entry")}} 被记录并且是指定的 `entryTypes` 之一的时候,性能观察者对象的回调函数会被调用。
- {{domxref("PerformanceObserver.disconnect","PerformanceObserver.disconnect()")}}
- : 性能监测回调停止接收 {{domxref("PerformanceEntry","性能条目", '', 'true')}}。

## 示例

```js
function perf_observer(list, observer) {
// Process the "measure" event
// 处理 "measure" 事件
}
var observer2 = new PerformanceObserver(perf_observer);
observer2.observe({entryTypes: ["measure"]});
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
69 changes: 0 additions & 69 deletions files/zh-cn/web/api/performanceobserver/observe/index.html

This file was deleted.

60 changes: 60 additions & 0 deletions files/zh-cn/web/api/performanceobserver/observe/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: PerformanceObserver.observe()
slug: Web/API/PerformanceObserver/observe
tags:
- 性能
- 性能监测对象
- 接口
translation_of: Web/API/PerformanceObserver/observe
---
{{APIRef("Performance Timeline API")}}

{{domxref("PerformanceObserver", "性能监测对象")}} 的 **`observe()`** 方法用于观察传入的参数中指定的性能条目类型的集合。当记录一个指定类型的性能条目时,性能监测对象的回调函数将会被调用。

## 语法

```js
observer.observe(options);
```

### 参数

- _options_
- : 一个只装了单个键值对的对象,该键值对的键名规定为 `entryTypes`。`entryTypes` 的取值要求如下:
- `entryTypes` 的值:一个放字符串的数组,字符串的有效值取值在{{domxref("PerformanceEntry.entryType", "性能条目类型")}} 中有详细列出。如果其中的某个字符串取的值无效,浏览器会自动忽略它。
- 另:若未传入 `options` 实参,或传入的 `options` 实参为空数组,会抛出 {{jsxref("TypeError")}}。

## 示例

```js
/* 写法一 */

//直接往 PerformanceObserver() 入参匿名回调函数,成功 new 了一个 PerformanceObserver 类的,名为 observer 的对象
var observer = new PerformanceObserver(function(list, obj) {
var entries = list.getEntries();
for (var i=0; i < entries.length; i++) {
//处理 “mark” 和 “frame” 事件
}
});
//调用 observer 对象的 observe() 方法
observer.observe({entryTypes: ["mark", "frame"]});

/* 写法二 */

//预先声明回调函数 perf_observer
function perf_observer(list, observer) {
//处理 “measure” 事件
}
//再将其传入 PerformanceObserver(),成功 new 了一个 PerformanceObserver 类的,名为 observer2 的对象
var observer2 = new PerformanceObserver(perf_observer);
//调用 observer2 对象的 observe() 方法
observer2.observe({entryTypes: ["measure"]});
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

This file was deleted.

Loading