SerialPort: sự kiện disconnect

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is available in Dedicated Web Workers.

Sự kiện disconnect của giao diện SerialPort được kích hoạt khi cổng ngắt kết nối khỏi thiết bị.

Mô tả

Cụ thể hơn, sự kiện disconnect kích hoạt khi một cổng đã từng kết nối logic sau khi người dùng cấp quyền cho trang web truy cập nó (sau lệnh gọi Serial.requestPort()) không còn kết nối nữa.

Nổi bọt sự kiện

Sự kiện này nổi bọt lên thực thể Serial đã trả về giao diện này. Thuộc tính event.target tham chiếu đến đối tượng SerialPort nổi bọt lên.

Để biết thêm thông tin, xem Nổi bọt sự kiện.

Cú pháp

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

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

ondisconnect = (event) => { }

Kiểu sự kiện

Một Event chung.

Ví dụ

Thông báo khi một cổng cụ thể ngắt kết nối

Ở đây trình lắng nghe sự kiện được cài đặt trên một đối tượng SerialPort cụ thể.

js
port.addEventListener("disconnect", (event) => {
  // notify that the port has become unavailable
});

Lắng nghe bất kỳ cổng nào trở nên không khả dụng

Sự kiện disconnect nổi bọt lên đối tượng Serial nơi bạn có thể lắng nghe bất kỳ cổng nào trở nên không khả dụng.

js
navigator.serial.addEventListener("disconnect", (event) => {
  // notify that a port has become unavailable
  // use `event.target` to refer to the unavailable port
});

Thông số kỹ thuật

Specification
Web Serial API
# dfn-disconnect
Web Serial API
# dom-serialport-ondisconnect

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

Xem thêm