PressureObserver

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.

Giao diện PressureObserver là một phần của Compute Pressure API và được dùng để quan sát các thay đổi áp lực của tài nguyên hệ thống như CPU.

Hàm khởi tạo

PressureObserver() Experimental

Tạo và trả về một đối tượng PressureObserver mới.

Thuộc tính tĩnh

PressureObserver.knownSources Read only Experimental

Trả về một mảng các giá trị source được user agent hỗ trợ.

Phương thức phiên bản

PressureObserver.observe() Experimental

Gọi hàm callback của pressure observer khi một bản ghi áp lực cho source được chỉ định được quan sát.

PressureObserver.unobserve() Experimental

Dừng hàm callback của pressure observer nhận bản ghi áp lực từ source được chỉ định.

PressureObserver.disconnect() Experimental

Dừng hàm callback của pressure observer nhận bản ghi áp lực từ tất cả các nguồn.

PressureObserver.takeRecords() Experimental

Trả về danh sách hiện tại các bản ghi áp lực được lưu trong pressure observer, đồng thời xóa rỗng nó.

Ví dụ

Ghi lại áp lực hiện tại

Ví dụ này tạo một PressureObserver và thực hiện hành động mỗi khi có thay đổi áp lực. Khoảng lấy mẫu được đặt thành 1000ms, nghĩa là sẽ có các cập nhật tối đa mỗi giây.

js
function callback(records) {
  const lastRecord = records[records.length - 1];
  console.log(`Current pressure ${lastRecord.state}`);
  if (lastRecord.state === "critical") {
    // disable video feeds
  } else if (lastRecord.state === "serious") {
    // disable video filter effects
  } else {
    // enable all video feeds and filter effects
  }
}

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

Thông số kỹ thuật

Specification
Compute Pressure Level 1
# the-pressureobserver-object

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

Xem thêm