判断页面有没有加载完成
2019-11-13 本文已影响0人
mindy1031
if(typeof window.addEventListener != "undefined") {
window.addEventListener('load', savePerformance); // 所有主流浏览器
} else if(document.all) {
window.attachEvent('onload', savePerformance); // ie
}
var count=0;
function savePerformance() {
if(window.performance) {
// 循环判断loadEventEnd不为0时即可获取指标值,否则可能出现为负值的情况
setTimeout(function() {
if(window.performance.timing.loadEventEnd!= 0|| count == 10) {
//页面加载完成了
} else {
savePerformance();
count = parseInt(count) + 1;
}
}, 1000);
} else {
console.log("当前浏览器不支持window.performance API");
}
}