PerformanceObserver: takeRecords() 方法

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2021 年 9 月起,所有瀏覽器均已支援此功能。

注意:此功能在 Web Workers 中可用。

takeRecords() 方法是 PerformanceObserver 介面的一部分,它會返回效能觀察器中儲存的當前 PerformanceEntry 物件列表,並清空該列表。

語法

js
takeRecords()

引數

無。

返回值

一個 PerformanceEntry 物件列表。

示例

獲取記錄

下面的示例將效能條目(performance entries)的當前列表儲存在 records 中,並清空效能觀察器。

js
const observer = new PerformanceObserver((list, obj) => {
  list.getEntries().forEach((entry) => {
    // Process "mark" and "measure" events
  });
});
observer.observe({ entryTypes: ["mark", "measure"] });
const records = observer.takeRecords();
console.log(records[0].name);
console.log(records[0].startTime);
console.log(records[0].duration);

規範

規範
效能時間線
# dom-performanceobserver-takerecords

瀏覽器相容性