HIDDevice: forget() 方法

可用性有限

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

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

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

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

HIDDevice 介面的 forget() 方法會關閉與 HID 裝置的連線並忘記該裝置。

語法

js
forget()

引數

無。

返回值

一個 Promise,一旦連線關閉、裝置被忘記且許可權被重置,它就會以 undefined 解析。

示例

在下面的示例中,我們連線到一個 Nintendo Switch Joy-Con Right HID 裝置,閃爍一次,然後斷開連線。

js
async function blink() {
  const devices = await navigator.hid.requestDevice({
    filters: [
      {
        vendorId: 0x057e, // Nintendo Co., Ltd
        productId: 0x2007, // Joy-Con Right
      },
    ],
  });
  const device = devices[0];
  await device.open();
  // Turn off
  await device.sendFeatureReport(reportId, Uint32Array.from([0, 0]));
  await new Promise((resolve) => setTimeout(resolve, 100));
  // Turn on
  await device.sendFeatureReport(reportId, Uint32Array.from([512, 0]));
  await new Promise((resolve) => setTimeout(resolve, 100));
  // Finally, disconnect from it
  await device.forget();
}
blink();

規範

規範
WebHID API
# dom-hiddevice-forget

瀏覽器相容性