PerformanceNavigationTiming: unloadEventStart 屬性
unloadEventStart 只讀屬性返回一個 DOMHighResTimeStamp,表示前一個文件的 unload 事件處理程式開始執行前的精確時間。
值
unloadEventStart 屬性可能具有以下值:
- 一個
DOMHighResTimeStamp,表示前一個文件的unload事件處理程式開始執行前的精確時間。 - 如果沒有前一個文件,則為
0。 - 如果前一個頁面來自不同的源,則為
0。
示例
測量 unload 事件處理程式的耗時
unloadEventStart 屬性可用於測量處理 unload 事件處理程式所需的時間。
這對於測量耗時長的 unload 事件處理程式非常有用。
js
window.addEventListener("unload", (event) => {
// Some long running code
});
使用 PerformanceObserver 的示例,它會在瀏覽器效能時間線中記錄新的 navigation 效能條目時通知您。使用 buffered 選項可以訪問觀察者建立之前的條目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const unloadEventTime = entry.unloadEventEnd - entry.unloadEventStart;
if (unloadEventTime > 0) {
console.log(
`${entry.name}: unload event handler time: ${unloadEventTime}ms`,
);
}
});
});
observer.observe({ type: "navigation", buffered: true });
使用 Performance.getEntriesByType() 的示例,它僅顯示在呼叫方法時瀏覽器效能時間線中存在的 navigation 效能條目。
js
const entries = performance.getEntriesByType("navigation");
entries.forEach((entry) => {
const loadEventTime = entry.unloadEventEnd - entry.unloadEventStart;
if (unloadEventTime > 0) {
console.log(`${entry.name}:
load event handler time: ${unloadEventTime}ms`);
}
});
規範
| 規範 |
|---|
| 導航計時 Level 2 # dom-performancenavigationtiming-unloadeventstart |
瀏覽器相容性
載入中…
另見
unload事件