【发布时间】:2020-07-29 17:11:35
【问题描述】:
我想获取PerformanceObserver 提供的数据并在我的应用中使用。
就我而言,我想从只负责css 文件的PerformanceObserver 获取数据并做一些事情(在这种情况下,只需检查这些资源是否被缓存)
代码示例如下:
class Styles {
static getStyleResources() {
const styles = [];
const po = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if(entry.initiatorType === 'css') {
styles.push(entry);
return styles;
}
}
});
po.observe({ type: 'resource', buffered: true });
}
static isChached() {
return Styles.getStyleResources().forEach(item => item.transferSize ?
console.log("The data is cached") :
console.log("The data is not cached")
)
}
}
我也尝试了不同的方法,但没有任何效果。有什么问题,我的方法是否正确?
【问题讨论】:
标签: javascript performance class monitoring