gpt4 book ai didi

javascript - 如何避免在网页返回时通过 JS 触发重复的 PubNub 通知

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

我在与 pubnub 一起使用时遇到 JS bootstrap 通知事件的问题。

当 BS 通知事件被触发时,我切换到另一个页面,然后返回到事件被触发的页面 - 事件再次被触发。就像它的缓存一样。比方说,JS 事件通过此页面上的 PubNub 触发了 4 次(例如在聊天中),我切换到另一个页面。

然后我在浏览器中返回到触发此事件的页面 - 我一下子收到了 4 次通知!

我的问题:如何避免在页面返回时触发 JS 事件?

bootstrap notify的JS代码:

$.notify({
icon: "/profile/getprofileimage/{0}/100".format(userid),
message: message
}, {
icon_type: 'image',
type: "success",
delay: 400,
allow_dismiss: false,
animate: {
enter: 'animated bounceInDown',
exit: 'animated bounceOutUp'
}
});

PubNub:

channels['request-received-<?php echo $_SESSION["user"]["id"]; ?>'] = function (data) {
notify(data.Message, data.From);
play('newpending');

if (typeof(hoodlister) !== "undefined" && hoodlister instanceof list)
hoodlister.getItem(data.From).then(updateProfile);
if (typeof(visitorlister) !== "undefined" && visitorlister instanceof list)
visitorlister.getItem(data.From).then(updateProfile);
if (typeof(pendingmatcheslister) !== "undefined" && pendingmatcheslister instanceof list)
pendingmatcheslister.refresh();
};

最佳答案

也许根据消息的时间标记保留警报缓存可以解决您的问题。您可以在触发警报之前检查缓存。

PubNub 时间 token (17 位数字)在消息量较低时几乎是唯一的,因此虽然这不是很优雅,但它可能是您需要的简单修复。

如果您触发大量警报并且浏览器选项卡已使用数天,则缓存可能会变大,因此您可能需要对此进行处理。

let cache = {};

pubnub.addListener({
message: function(event) {
if (!cache[event.timetoken]) {
cache[event.timetoken] = true;
// trigger the alert here
}
}
});

关于javascript - 如何避免在网页返回时通过 JS 触发重复的 PubNub 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55798731/

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