Back to Content

MessagePort: close() method

files/en-us/web/api/messageport/close/index.md

latest973 B
Original Source

{{APIRef("Channel Messaging API")}} {{AvailableInWorkers}}

The close() method of the {{domxref("MessagePort")}} interface disconnects the port, so it is no longer active. This stops the flow of messages to that port.

Syntax

js-nolint
close()

Parameters

None.

Return value

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

Examples

In the following code block, you can see a handleMessage handler function, run when a message is sent back to this document using {{domxref("EventTarget.addEventListener")}}.

js
channel.port1.addEventListener("message", handleMessage);
function handleMessage(e) {
  para.innerHTML = e.data;
  textInput.value = "";
}

channel.port1.start();

You could stop messages being sent at any time using

js
channel.port1.close();

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also