1

テキスト ファイルから URL を読み取り、それらを iframe に次々と配置するコードを取得しました。各ページの読み込み時間を計算したい。これは、iframe が URL の読み込みを開始する前の時間をマークし、読み込まれた直後の時間をマークすることによって行います。ロード後マイナスロード前は、各ページのロード時間を教えてくれます。

    $.get("text.txt", function (data) {
        var array = data.split(/\r\n|\r|\n/) 
        var beforeLoad = (new Date()).getTime();    
        var loadTimes = [];
    $('#1').on('load', function () {
        loadTimes.push((new Date()).getTime());           
        $('#1').attr('src', array.pop()); 
            $.each(loadTimes, function (index, value) {             
                var result = (value - beforeLoad) / 1000;       
                $("#loadingtime" + index).html(result);
            }); 
    }).attr('src', array.pop());
});

ここでの問題 - ロード時間の前。各 URL の読み込みが開始されるまでの時間をマークする方法がわかりません。上記のコードでは、beforeload 値は、すべてのソースがロードされる前の時間値です。ここで、新しいソースが iframe に配置されるたびに変更する必要があります。これを行う方法について何か提案はありますか?

編集:修正しました!

   $.get("imones.txt", function (data) {
        var array = data.split(/\r\n|\r|\n/) 
        var beforeLoad = (new Date()).getTime();    
        var loadTimes = [];
            var beforeTimes = [];               
        $('#frame_id').on('load', function () {                                 
            beforeTimes.push(beforeLoad);
            loadTimes.push((new Date()).getTime()); 
            $('#frame_id').attr('src', array.pop());
                $.each(loadTimes, function (index, value) { 
                    var result = (value - beforeTimes[index]) / 1000;
                        if (result < 0) {
                            result = result * (-1);
                        }   
                    $("#loadingtime" + index).html(result);
                    beforeLoad = value;
                });
        }).attr('src', array.pop());
    });

私がそこで何をしたかわかりませんが、これは私にとってはうまくいきます。今それを分析します。各ページの読み込み時間の前に、正しいストアの配列を作成しました。

4

4 に答える 4

1
var loadTimes = {};
$('#1').data('time', (new Date()).getTime()).on('load', function() {
  var $this = $(this), time = (new Date()).getTime();
  loadTimes[$this.attr('src')] = time - $this.data('time');
  $this.data('time', time);
  if (array.length == 0) {
    //end of load loadTimes contents with time of load each url
  } else $this.attr('src', array.pop());
}).attr('src', array.pop());
于 2013-02-26T05:34:44.103 に答える
0

新しいbeforeLoad時間を一時的に保存し、最後のロード時間を計算し、新しいbeforeLoad時間をコピーします。

$.get("text.txt", function (data) {
  var array = data.split(/\r\n|\r|\n/); 
  var beforeLoad = (new Date()).getTime();    
  var loadTimes = [];

  $('#1').on('load', function () {

    var nextBeforeLoad = (new Date()).getTime();
    loadTimes.push(nextBeforeLoad);       

    $.each(loadTimes, function (index, value) {             
        var result = (value - beforeLoad) / 1000;       
        $("#loadingtime" + index).html(result);
      }); 

    beforeLoad = nextBeforeLoad;
    $('#1').attr('src', array.pop()); 

  }).attr('src', array.pop());
});
于 2013-02-26T05:38:01.520 に答える
0
  $.get("text.txt", function (data) {
        var array = data.split(/\r\n|\r|\n/) 
        var beforeLoad = (new Date()).getTime();    
        var loadTimes = [];
            var beforeTimes = [];               
        $('#frame_id').on('load', function () {                                 
            beforeTimes.push(beforeLoad);
            loadTimes.push((new Date()).getTime()); 
            $('#frame_id').attr('src', array.pop());
                $.each(loadTimes, function (index, value) { 
                    var result = (value - beforeTimes[index]) / 1000;
                        if (result < 0) {
                            result = result * (-1);
                        }   
                    $("#loadingtime" + index).html(result);
                    beforeLoad = value;
                });
        }).attr('src', array.pop());
    });
于 2013-02-26T07:02:01.997 に答える
0

これを試して

var links = [
    'http://google.com',
    'http://yahoo.com'
]

$(links).each(function (i) {
    var iframe = document.createElement('iframe');
    iframe.setAttribute('t1', (new Date()).getTime());
    iframe.setAttribute('src', links[i]);
    iframe.onload = function () {
        var t2 = (new Date()).getTime();
        console.log('Load Time: ' + ( t2 - parseInt(this.getAttribute('t1'))));
    }
    document.body.appendChild(iframe);
});
于 2013-02-26T06:37:49.967 に答える