BroadcastChannel:close() 方法

Baseline Widely available

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

备注: 此特性在 Web Worker 中可用。

BroadcastChannel 接口的 close() 方法终止与底层频道的连接,从而允许对对象进行垃圾回收。这是必须执行的步骤,因为浏览器没有其他方法可以知道不再需要此频道。

语法

js
close()

示例

js
// 连接到指定频道
const bc = new BroadcastChannel("test_channel");

// 其他操作(如:postMessage、……)

// 当完成后,断开与频道的连接
bc.close();

规范

Specification
HTML
# dom-broadcastchannel-close-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
close

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
User must explicitly enable this feature.

参见