PerformanceElementTiming: toJSON() method
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Phương thức toJSON() của giao diện PerformanceElementTiming là một bộ tuần tự hóa; nó trả về biểu diễn JSON của đối tượng PerformanceElementTiming.
Cú pháp
toJSON()
Tham số
Không có.
Giá trị trả về
Một đối tượng JSON là dạng tuần tự hóa của đối tượng PerformanceElementTiming.
JSON không chứa thuộc tính element vì nó thuộc kiểu Element, kiểu này không cung cấp thao tác toJSON(). Tuy nhiên, id của phần tử được cung cấp.
Ví dụ
>Sử dụng phương thức toJSON
Trong ví dụ này, gọi entry.toJSON() trả về biểu diễn JSON của đối tượng PerformanceElementTiming, với thông tin về phần tử hình ảnh.
<img
src="image.jpg"
alt="a nice image"
elementtiming="big-image"
id="myImage" />
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
if (entry.identifier === "big-image") {
console.log(entry.toJSON());
}
});
});
observer.observe({ type: "element", buffered: true });
Kết quả sẽ là một đối tượng JSON như sau:
{
"name": "image-paint",
"entryType": "element",
"startTime": 670894.1000000238,
"duration": 0,
"renderTime": 0,
"loadTime": 670894.1000000238,
"intersectionRect": {
"x": 299,
"y": 76,
"width": 135,
"height": 155,
"top": 76,
"right": 434,
"bottom": 231,
"left": 299
},
"identifier": "big-image",
"naturalWidth": 135,
"naturalHeight": 155,
"id": "myImage",
"url": "https://en.wikipedia.org/static/images/project-logos/enwiki.png"
}
Để lấy chuỗi JSON, bạn có thể sử dụng JSON.stringify(entry) trực tiếp; nó sẽ tự động gọi toJSON().
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Element Timing API> # dom-performanceelementtiming-tojson> |