BroadcastChannel:close() 方法

Baseline 已廣泛支援

此特性已經十分成熟,可在許多裝置和瀏覽器版本上使用。自 2022 年 3 月起,它已在各瀏覽器中可用。

注意:此功能在 Web Workers 中可用。

close() 方法是 BroadcastChannel 介面的一個方法,用於終止與底層通道的連線,從而允許該物件被垃圾回收。這是一個必要的步驟,因為瀏覽器沒有其他方法知道不再需要這個通道。

語法

js
close()

引數

無。

返回值

無(undefined)。

示例

js
// Connect to a channel
const bc = new BroadcastChannel("test_channel");

// More operations (like postMessage, …)

// When done, disconnect from the channel
bc.close();

規範

規範
HTML
# dom-broadcastchannel-close-dev

瀏覽器相容性

另見