EventSource: close() method

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("Server Sent Events")}}{{AvailableInWorkers}}

The **close()** method of the {{domxref("EventSource")}} interface closes the connection, if one is made, and sets the {{domxref("EventSource.readyState")}} attribute to 2 (closed).

توجه: If the connection is already closed, the method does nothing.

Syntax

js-nolint
close()

Parameters

None.

Return value

None ({{jsxref("undefined")}}).

Examples

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

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

توجه: You can find a full example on GitHub — see Simple SSE demo using PHP.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("EventSource")}}