MessagePort.close()

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

Note: This feature is available in Web Workers

Syntax

port.close()

Returns

Void.

Parameters

None.

Example

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

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

channel.port1.start();

You could stop messages being sent at any time using

channel.port1.close();

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
close
4
12
41
10
10.6
5
≤37
18
41
11
4.2
1.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/close