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.
addEventListener("connect", (event) => { })
onconnect = (event) => { }
Kiểu sự kiện
Một USBConnectionEvent. Kế thừa từ Event.
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.
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.
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:
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> |