gpt4 book ai didi

javascript - 具有localStorage的BroadcastChannel-兼容性

转载 作者:行者123 更新时间:2023-11-30 14:31:37 25 4
gpt4 key购买 nike

当收到新消息时,我在项目中使用BroadcastChannel更改所有选项卡的标题。
问题在于它仅适用于chrome和firefox。因此,我决定使用localStorage创建一个BroadcastChannel:

setInterval(function() {

// check new messages:

var title = localStorage.getItem('title');
if(title != null && title != 0){

document.title = document.title.replace(/\(\d+\)\s+/, "");
document.title='('+title+')' + " " + document.title;

}
localStorage.setItem("title", "0");

}, 100);


并且带有的ajax将检查消息,如果有一些新消息:

localStorage.setItem("title", i);


问题是,它正在与某些选项卡添加(1)一起工作,但有些却没有...我想是因为在所有选项卡都可以读取它之前,它正在重置localStorage setItem title = 0。有什么想法可以使它可用,直到所有选项卡都可以阅读?

最佳答案

不要使用间隔读取本地存储,而应使用每次调用setItem时都会触发的“存储”事件。

对于真正适用于所有浏览器的BroadcastChannel,我建议https://github.com/pubkey/broadcast-channel

关于javascript - 具有localStorage的BroadcastChannel-兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com