Performance:getEntriesByType() 方法
注意:此功能在 Web Workers 中可用。
getEntriesByType() 方法會返回效能時間線上當前存在的、給定型別的 PerformanceEntry 物件陣列。
如果您對特定名稱的效能條目感興趣,請參閱 getEntriesByName()。要獲取所有效能條目,請參閱 getEntries()。
注意:此方法不會通知您新的效能條目;您只會獲得在呼叫此方法時效能時間軸中存在的條目。要接收有關可用條目的通知,請使用 PerformanceObserver。
以下條目型別完全不受此方法支援,即使可能存在這些型別的條目也不會返回
"element"(PerformanceElementTiming)"event"(PerformanceEventTiming)"largest-contentful-paint"(LargestContentfulPaint)"layout-shift"(LayoutShift)"longtask"(PerformanceLongTaskTiming)
要訪問這些型別的條目,您必須改用 PerformanceObserver。
語法
js
getEntriesByType(type)
引數
type-
要檢索的條目型別,例如
"mark"。有效的條目型別列在PerformanceEntry.entryType中。支援的entryTypes可以使用靜態屬性PerformanceObserver.supportedEntryTypes進行檢索。
返回值
一個 PerformanceEntry 物件陣列,這些物件的 type 與指定的型別匹配。這些條目將根據條目的 startTime 按時間順序排列。如果沒有具有指定 type 的物件,或者未提供引數,則返回一個空陣列。
示例
記錄資源條目
以下示例記錄所有型別為 "resource" 的條目。
js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
console.log(`${entry.name}'s startTime: ${entry.startTime}`);
});
規範
| 規範 |
|---|
| 效能時間線 # dom-performance-getentriesbytype |
瀏覽器相容性
載入中…