diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/performanceobserver/observe | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/performanceobserver/observe')
-rw-r--r-- | files/zh-cn/web/api/performanceobserver/observe/index.html | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/performanceobserver/observe/index.html b/files/zh-cn/web/api/performanceobserver/observe/index.html new file mode 100644 index 0000000000..a397f4dda3 --- /dev/null +++ b/files/zh-cn/web/api/performanceobserver/observe/index.html @@ -0,0 +1,134 @@ +--- +title: PerformanceObserver.observe() +slug: Web/API/PerformanceObserver/observe +tags: + - 性能 + - 性能监测对象 + - 接口 +translation_of: Web/API/PerformanceObserver/observe +--- +<div>{{APIRef("Performance Timeline API")}}</div> + +<p>{{domxref("PerformanceObserver", "性能监测对象")}} 的 <strong><code>observe()</code></strong> 方法用于观察传入的参数中指定的性能条目类型的集合。当记录一个指定类型的性能条目时,性能监测对象的回调函数将会被调用。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>observer</em>.observe(<em>options</em>); +</pre> + +<h3 id="参数">参数</h3> + +<p> </p> + +<dl> + <dt><em>options</em></dt> + <dd>一个只装了单个键值对的对象,该键值对的键名规定为 <code>entryTypes</code>。<code>entryTypes</code> 的取值要求如下:</dd> +</dl> + +<ul> + <li><code>entryTypes</code> 的值:一个放字符串的数组,字符串的有效值取值在{{domxref("PerformanceEntry.entryType", "性能条目类型")}} 中有详细列出。如果其中的某个字符串取的值无效,浏览器会自动忽略它。</li> + <li>另:若未传入 <code>options</code> 实参,或传入的 <code>options</code> 实参为空数组,会抛出 {{jsxref("TypeError")}}。</li> +</ul> + +<dl> + <dt> </dt> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: 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"]}); +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Performance Timeline Level 2', '#dom-performanceobserver-observe', 'observe()')}}</td> + <td>{{Spec2('Performance Timeline Level 2')}}</td> + <td>Initial definition of <code>observe()</code> method.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(52.0)}}</td> + <td>{{CompatGeckoDesktop(57)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatOpera("39")}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile(57)}}</td> + <td>{{CompatNo}}</td> + <td> + <p>{{CompatOperaMobile(39)}}</p> + </td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(52.0)}}</td> + </tr> + </tbody> +</table> +</div> |