您的位置:首页 > 其它

计算首屏加载完毕的时间

2019-07-30 02:28 281 查看
原文链接:http://www.cnblogs.com/joyho/articles/4708995.html
function getOffsetTop(ele) {
var offsetTop = ele.offsetTop;
if (ele.offsetParent !== null) {
offsetTop += getOffsetTop(ele.offsetParent);
}
return offsetTop;
}

var windowHeight = window.innerHeight,
firstScreenImgs = [],
ary = [],
allImgLoaded = false,
allImg = document.getElementsByTagName('img');

for(var i= 0,len=allImg.length;i<len;i++){//筛选出首屏区域内的图片
var offsetTop = getOffsetTop(allImg[i]);
if(offsetTop < windowHeight){//首屏区域内
firstScreenImgs.push(allImg[i]);
}
}
var len = firstScreenImgs.length;

for(var i=0;i<firstScreenImgs.length;i++){
(function(j){
if(firstScreenImgs[j].complete){
len--;
}
else{
firstScreenImgs[j].onload = function(){
len--;
ary.push({index:j,time:parseInt(+new Date)});
if(len == 0){
window.firstScreenEndTime = 0;
for(var k in ary){
if(ary[k].time > firstScreenEndTime){
firstScreenEndTime = ary[k].time;
}
}
}
}
}
})(i)
}

 

转载于:https://www.cnblogs.com/joyho/articles/4708995.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: