gpt4 book ai didi

javascript - 在 html 页面运行时解析 javascript 中挂起的外部资源?

转载 作者:可可西里 更新时间:2023-11-01 16:37:10 26 4
gpt4 key购买 nike

问题:

我怎么能说计算页面在 javascript 中创建的待处理资源的总数?

GetEntriesByType这是包含一些图像和 http 请求的页面的快速片段:

<div id="results"></div>
<img src="http://placekitten.com/g/200/100" alt=""/> //resource 1
<img src="http://placekitten.com/g/240/500" alt=""/> //resource 2
<img src="http://placekitten.com/g/210/300" alt=""/> //resource 3
<img src="http://placekitten.com/g/500/300" alt=""/> //resource 4
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script> //resource 5
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script> //resource 6
<script>
var results = document.getElementById('results');
var r = new XMLHttpRequest();
r.open("GET", "http://www.filltext.com?rows=10&f={firstName}", true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
var data = JSON.parse(r.responseText);
for (i = 0; i < data.length; i++) {
results.innerHTML += '<li>' + data[i].f + '</li>'
}
};
r.send(); //resource 7
var hey = window.performance.getEntriesByType("resource");
console.log(hey); //returns 0 :( which I hope would be 7 before dom renders.
</script>

最佳答案

为@Jaromanda +1。

试试这个:

setTimeout(function(){
var hey = window.performance.getEntriesByType("resource");
console.log(hey);
}, 0);

http://jsfiddle.net/8bgwma8n/1/

关于javascript - 在 html 页面运行时解析 javascript 中挂起的外部资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050388/

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