NetworkInformation: change 事件

可用性有限

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

注意:此功能在 Web Workers 中可用。

NetworkInformation 介面的 change 事件在連線資訊發生變化時觸發,該事件由 NetworkInformation 物件接收。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("change", (event) => { })

onchange = (event) => { }

事件型別

一個通用的 Event

示例

js
// Get the connection type.
const type = navigator.connection.type;

function changeHandler(e) {
  // Handle change of connection type here.
}

// Register for event changes:
navigator.connection.onchange = changeHandler;

// Another way: navigator.connection.addEventListener('change', changeHandler);

規範

規範
Network Information API
# dom-networkinformation-onchange

瀏覽器相容性