PerformanceNavigationTiming: loadEventStart 属性

Baseline 已广泛支持

此功能已成熟,并且适用于多种设备和浏览器版本。自 2021 年 10 月以来,它已在所有浏览器中可用。

loadEventStart 只读属性返回一个 DOMHighResTimeStamp,表示当前文档的 load 事件处理程序开始前的精确时间。

一个 DOMHighResTimeStamp,表示当前文档的 load 事件处理程序开始前的精确时间。

示例

测量 load 事件处理程序的时间

loadEventStart 属性可用于测量处理 load 事件处理程序所需的时间。

这对于测量耗时长的 load 事件处理程序的时间非常有用。

js
window.addEventListener("load", (event) => {
  // Some long running code
});

使用 PerformanceObserver 的示例,它会在浏览器性能时间线中记录新的 navigation 性能条目时通知您。使用 buffered 选项可以访问观察者创建之前的条目。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    const loadEventTime = entry.loadEventEnd - entry.loadEventStart;
    if (loadEventTime > 0) {
      console.log(`${entry.name}: load event handler time: ${loadEventTime}ms`);
    }
  });
});

observer.observe({ type: "navigation", buffered: true });

使用 Performance.getEntriesByType() 的示例,它仅显示在调用方法时浏览器性能时间线中存在的 navigation 性能条目。

js
const entries = performance.getEntriesByType("navigation");
entries.forEach((entry) => {
  const loadEventTime = entry.loadEventEnd - entry.loadEventStart;
  if (loadEventTime > 0) {
    console.log(`${entry.name}:
      load event handler time: ${loadEventTime}ms`);
  }
});

规范

规范
导航计时 Level 2
# dom-performancenavigationtiming-loadeventstart

浏览器兼容性

另见