gpt4 book ai didi

Javascript 获取每个 iframe 源的加载时间

转载 作者:行者123 更新时间:2023-11-28 02:25:07 25 4
gpt4 key购买 nike

我得到了一段代码,可以从文本文件中读取网址并将它们依次放入 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());
});

这里出现问题 - 在加载时间之前。我不知道如何在每个网址开始加载之前标记时间。在上面的代码中,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());
});

不确定我在那里做了什么,但这对我有用,现在要分析它。我在每个页面加载时间之前制作了一个正确存储的数组。

最佳答案

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());

关于Javascript 获取每个 iframe 源的加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15081722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com