PerformanceEventTiming: interactionId 屬性

可用性有限

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

只讀的 interactionId 屬性是 PerformanceEventTiming 介面的一部分,它返回一個唯一標識使用者互動的 ID,該互動觸發了一系列相關的事件。

一個數字。對於未計算互動 ID 的事件型別,值為 0。

描述

當用戶與網頁互動時,一次使用者互動(例如點選)通常會觸發一系列事件,如 pointerdownpointerupclick 事件。為了衡量這一系列事件的延遲,這些事件會共享同一個 interactionId

interactionId 僅為屬於使用者互動的以下事件型別計算。否則,它為 0

事件型別 使用者互動
pointerdownpointerupclick 點選 / 輕觸 / 拖動
keydownkeyup 按鍵

interactionId 也用於計算 Interaction to next paint(互動到下一次繪製)指標,該指標有助於分析頁面生命週期內使用者互動的響應能力。

示例

使用 interactionId

以下示例收集與一次互動相關的所有事件的持續時間。然後,可以使用 eventLatencies 地圖來查詢一次使用者互動事件的最大持續時間,例如。

js
// The key is the interaction ID.
let eventLatencies = {};

const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    if (entry.interactionId > 0) {
      const interactionId = entry.interactionId;
      if (!eventLatencies.has(interactionId)) {
        eventLatencies[interactionId] = [];
      }
      eventLatencies[interactionId].push(entry.duration);
    }
  });
});

observer.observe({ type: "event", buffered: true });

// Log events with maximum event duration for a user interaction
Object.entries(eventLatencies).forEach(([k, v]) => {
  console.log(Math.max(...v));
});

規範

規範
事件計時 API
# dom-performanceeventtiming-interactionid

瀏覽器相容性