PerformanceScriptTiming: 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 PerformanceScriptTiming là một bộ tuần tự hóa; nó trả về biểu diễn JSON của đối tượng PerformanceScriptTiming.

Cú pháp

js
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 PerformanceScriptTiming.

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 PerformanceScriptTiming đầu tiên có sẵn trong một khung hoạt hình dài được quan sát.

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log(entry.scripts[0].toJSON());
  });
});

observer.observe({ type: "long-animation-frame", buffered: true });

Kết quả sẽ là một đối tượng như sau:

js
({
  duration: 45,
  entryType: "script",
  executionStart: 11803.199999999255,
  forcedStyleAndLayoutDuration: 0,
  invoker: "DOMWindow.onclick",
  invokerType: "event-listener",
  name: "script",
  pauseDuration: 0,
  sourceURL: "https://web.dev/js/index-ffde4443.js",
  sourceFunctionName: "myClickHandler",
  sourceCharPosition: 17796,
  startTime: 11803.199999999255,
  windowAttribution: "self",
});

Để 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
Long Animation Frames API
# dom-performancescripttiming-tojson

Tương thích trình duyệt

Xem thêm