PressureRecord

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

注意:此功能在 Web Workers 中可用,但 Service Workers 除外。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

PressureRecord 介面是 Compute Pressure API 的一部分,用於描述在特定過渡時刻的源的壓力趨勢。

例項屬性

PressureRecord.source 只讀 實驗性

一個字串,指示記錄的來源。

PressureRecord.state 只讀 實驗性

一個字串,指示記錄的壓力狀態。

PressureRecord.time 只讀 實驗性

一個 DOMHighResTimeStamp,表示記錄的時間戳。

例項方法

PressureRecord.toJSON() 實驗性

返回 PressureRecord 物件的 JSON 表示形式。

示例

使用 PressureRecord 物件

在下面的示例中,我們在壓力觀察者回調中記錄 PressureRecord 物件的屬性。

js
function callback(records) {
  const lastRecord = records[records.length - 1];
  console.log(`Current pressure is ${lastRecord.state}`);
  console.log(`Current pressure observed at ${lastRecord.time}`);
  console.log(`Current pressure source: ${lastRecord.source}`);
}

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

規範

規範
計算壓力級別 1
# the-pressurerecord-interface

瀏覽器相容性