EventSource: phương thức close()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Note: This feature is available in Web Workers.

Phương thức close() của giao diện EventSource đóng kết nối, nếu có, và đặt thuộc tính EventSource.readyState thành 2 (closed).

Note: Nếu kết nối đã đóng, phương thức này không làm gì cả.

Cú pháp

js
close()

Tham số

Không có.

Giá trị trả về

Không có (undefined).

Ví dụ

js
const button = document.querySelector("button");
const evtSource = new EventSource("sse.php");

button.onclick = () => {
  console.log("Connection closed");
  evtSource.close();
};

Note: Bạn có thể tìm thấy một ví dụ đầy đủ trên GitHub, xem Simple SSE demo using PHP.

Thông số kỹ thuật

Thông số kỹ thuật
HTML
# dom-eventsource-close-dev

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

Xem thêm