PerformanceLongAnimationFrameTiming: toJSON() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

toJSON() 方法是 PerformanceLongAnimationFrameTiming 介面的一個 序列化器;它返回 PerformanceLongAnimationFrameTiming 物件的 JSON 表示形式。

語法

js
toJSON()

引數

無。

返回值

一個 JSON 物件,它是 PerformanceLongAnimationFrameTiming 物件的序列化表示。

示例

使用 toJSON 方法

在此示例中,呼叫 entry.toJSON() 會返回 PerformanceLongAnimationFrameTiming 物件的 JSON 表示形式。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log(entry.toJSON());
  });
});

observer.observe({ type: "long-animation-frame", buffered: true });

這將像這樣記錄一個物件:

js
({
  blockingDuration: 0,
  duration: 60,
  entryType: "long-animation-frame",
  firstUIEventTimestamp: 11801.099999999627,
  name: "long-animation-frame",
  renderStart: 11858.800000000745,
  scripts: [
    {
      duration: 45,
      entryType: "script",
      executionStart: 11803.199999999255,
      forcedStyleAndLayoutDuration: 0,
      invoker: "DOMWindow.onclick",
      invokerType: "event-listener",
      name: "script",
      pauseDuration: 0,
      sourceURL: "https://web.dev/js/index-ffde4443.js",
      sourceFunctionName: "myClickHandler",
      sourceCharPosition: 17796,
      startTime: 11803.199999999255,
      window: {
        // …Window object…
      },
      windowAttribution: "self",
    },
  ],
  startTime: 11802.400000000373,
  styleAndLayoutStart: 11858.800000000745,
});

要獲取 JSON 字串,您可以直接使用 JSON.stringify(entry);它會自動呼叫 toJSON()

規範

規範
Long Animation Frames API
# dom-performancelonganimationframetiming-tojson

瀏覽器相容性

另見