我正在使用 Navigation Timing API 从页面获取加载事件。我在下面添加了 JS 的片段来输出信息。我注意到奇怪的一件事是时间比我检查控制台loadEventEnd
的时间早回来。loadEventStart
我认为这应该是不可能的。
var startTime = new Date().getTime();
// retrieve the performance object in a cross browser way. Check window.performance first.
window.performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {};
var timing = window.performance.timing || {};
var navigation = window.performance.navigation || {};
// if the Navigation Timing API is supported
if (window.performance && window.performance.timing) {
pageRequestStart = timing.requestStart;
pageResponseStart = timing.responseStart;
pageResponseEnd = timing.responseEnd;
pageLoadEventStart = timing.loadEventStart;
pageLoadEventEnd = timing.loadEventEnd;
pageLoadTime = timing.navigationStart;
}
var timingOutput =
"requestStart: " + pageRequestStart + "\n"
+ "responseStart: " + pageResponseStart + "\n"
+ "responseEnd: " + pageResponseEnd + "\n"
+ "loadEventStart: " + pageLoadEventStart + "\n"
+ "loadEventEnd: " + pageLoadEventEnd + "\n"
+ "navigationStart: " + pageLoadTime;
console.log(timingOutput);