PressureObserver: phương thức observe()
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.
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 observe() của giao diện PressureObserver yêu cầu pressure observer bắt đầu quan sát các thay đổi áp lực. Sau khi phương thức này được gọi, observer sẽ gọi hàm callback của nó khi một bản ghi áp lực cho source được chỉ định được quan sát.
Khi nhận được PressureRecord phù hợp, hàm callback của pressure observer sẽ được gọi.
Cú pháp
observe(source)
observe(source, options)
Tham số
source-
Một chuỗi chỉ định
sourcenào cần quan sát. XemPressureRecord.sourceđể biết danh sách các nguồn vàPressureObserver.knownSourcesđể biết danh sách các nguồn mà user agent hỗ trợ. optionsOptional-
Một đối tượng để cấu hình việc quan sát với các thuộc tính sau:
sampleIntervalOptional-
Một số đại diện cho khoảng thời gian lấy mẫu được yêu cầu, tính bằng mili giây. Mặc định là 0, nghĩa là sẽ nhận cập nhật nhanh nhất mà hệ thống có thể xử lý.
Giá trị trả về
Ngoại lệ
NotAllowedErrorDOMException-
Được ném ra nếu Compute Pressure API bị từ chối bởi
compute-pressurePermissions Policy. NotSupportedErrorDOMException-
Được ném ra nếu tham số
sourcekhông phải là một trong các nguồn được hỗ trợ cho user agent này.
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.
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
| Thông số kỹ thuật |
|---|
| Compute Pressure Level 1> # the-observe-method> |