dongqi3533 2013-04-04 06:54
浏览 27

使用javascript计算多个iframe的加载时间

First i create an iframe for each url i have:

$url = array();
$url[] = 'example1.com';
$url[] = 'example2.com';

foreach($url as $u){
   echo '<div class="frame_container">
         <iframe  "width="200" height="200" id="myiframe" src="http://www.'.$u.'">  </iframe>
         <div class="loadingtime"></div>
         </div>';
}

Then i have this javascript that should calculate the time before each frame starts loading, and the time after its loaded:

$(document).ready(function () {
load_time(); 
});
function load_time(){
var beforeLoad = (new Date()).getTime();
$('.frame_container iframe').on('load', function() {
    var afterLoad = (new Date()).getTime();
    var result = (afterLoad - beforeLoad) / 1000;
    // beforeLoad = afterLoad;
    $(this).parent().find('div.loadingtime').html(result);
});
}

My problem - the beforeLoad value is the same for all iframes. This value should be different for each frame because all fames dont start loading at the exact same time. Any suggestions?

  • 写回答

1条回答 默认 最新

  • dqy012345 2013-04-04 10:26
    关注

    write first div using php then remaining using your javascript code

    var curFrame = 1;
    var totalIFrame = 2;
    var url = new Array('http://www.abc.com','http://www.xyz.com');
    
    var beforeLoad = (new Date()).getTime();
    var result = 0;
    
    function addTime() {
        var afterLoad = (new Date()).getTime();
        result += ((afterLoad - beforeLoad) / 1000);
    
        if(totalIFrame==curFrame){// number of iframe you will be loading
            $(this).parent().find('div.loadingtime').html(result);
        } else {
            $('.frame_container').append('<iframe  "width="200" height="200" id="myiframe" src="'+url[curFrame]+'" onload="addTime()">  </iframe>');
        }
    
        curFrame++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)