PressureRecord: toJSON() method

Limited availability

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.

Note: This feature is available in Web Workers, except for Service Workers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Phương thức toJSON()bộ tuần tự hóa; nó trả về biểu diễn JSON của đối tượng PressureRecord.

Cú pháp

js
toJSON()

Tham số

Không có.

Giá trị trả về

Một đối tượng JSON là bản tuần tự hóa của đối tượng PressureRecord.

Ví dụ

Sử dụng phương thức toJSON

Trong ví dụ này, việc gọi lastRecord.toJSON() trả về biểu diễn JSON của đối tượng PressureRecord.

js
function callback(records) {
  const lastRecord = records[records.length - 1];
  console.log(lastRecord.toJSON);
}

try {
  const observer = new PressureObserver(callback);
  await observer.observe("cpu", {
    sampleInterval: 1000, // 1000ms
  });
} catch (error) {
  // report error setting up the observer
}

Đây sẽ ghi lại một đối tượng JSON như sau:

json
{
  "source": "cpu",
  "state": "fair",
  "time": 1712052746385.347
}

Để lấy chuỗi JSON, bạn có thể sử dụng JSON.stringify(lastRecord) trực tiếp; nó sẽ tự động gọi toJSON().

Thông số kỹ thuật

Specification
Compute Pressure Level 1
# the-tojson-member

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

Xem thêm