USB: sự kiện connect

Limited availability

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.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Sự kiện connect của giao diện USB được phát ra bất cứ khi nào một thiết bị đã ghép đôi được kết nối.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt một thuộc tính trình xử lý sự kiện.

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

onconnect = (event) => { }

Kiểu sự kiện

Một USBConnectionEvent. Kế thừa từ Event.

Event USBConnectionEvent

Thuộc tính sự kiện

Ngoài các thuộc tính được liệt kê bên dưới, các thuộc tính từ giao diện cha Event cũng có sẵn.

device Read only

USBDevice mà sự kiện được phát ra cho nó.

Ví dụ

Khi một thiết bị USB được kết nối, bạn có thể muốn cập nhật giao diện người dùng.

js
navigator.usb.addEventListener("connect", (event) => {
  // Thêm event.device vào giao diện người dùng.
});

Hoặc bạn có thể dùng thuộc tính trình xử lý sự kiện USB.onconnect để thiết lập trình xử lý cho sự kiện connect:

js
navigator.usb.onconnect = (event) => {
  // Thêm event.device vào giao diện người dùng.
};

Thông số kỹ thuật

Specification
WebUSB API
# connect
WebUSB API
# dom-usb-onconnect

Tương thích trình duyệt