如何在按下确定后停止显示javascript警报

时间:2022-02-11 19:49:30

I want to show an alert if I have something in my Facebook inbox. I think it can be easily accomplished using userscripts... this is what I have so far (thanks to the guys at the userscripts forums):

如果我的Facebook收件箱中有东西,我想显示警告。我认为使用用户脚本可以很容易地完成...这是我到目前为止(感谢用户论坛上的人):

document.addEventListener("DOMNodeInserted", function() {
  var count;
  if ((count=parseInt(document.getElementById("fb_menu_inbox_unread_count").textContent)) > 0)
  alert("You have "+count+" new message"+(count==1 ? "" : "s")+".");
}, true);

This works great, except the message gets stuck in a loop after clicking "OK", and keeps popping up. Is there a way to stop the message after I click dismiss the alert?

这很好用,除非在单击“确定”后消息陷入循环,并且不断弹出。点击关闭警报后,有没有办法停止消息?

2 个解决方案

#1


Add a variable which keeps track of how many messages there were last alert, and do not show if that variable hasn't changed.

添加一个变量,用于跟踪上次警报的消息数量,并且不显示该变量是否未更改。

Something like:

document.addEventListener(
  "DOMNodeInserted", 
  function() { 
    var count = parseInt(document.getElementById("fb_menu_inbox_unread_count").textContent);
    if (count > 0 && count != lastCount) {
      alert("You have "+count+" new message"+(count==1 ? "" : "s")+"."); }, true);
    }
    lastCount = count;  // Remember count to avoid continuous alerts.

Also, I would avoid writing code all in one like, as you did in your original post. It makes it more difficult to read and change if need be.

另外,我会避免像在原始帖子中那样编写代码。如果需要,它会使阅读和更改变得更加困难。

#2


Set a custom cookie using document.cookie to save the count, then do your regular checks

使用document.cookie设置自定义cookie以保存计数,然后进行常规检查

#1


Add a variable which keeps track of how many messages there were last alert, and do not show if that variable hasn't changed.

添加一个变量,用于跟踪上次警报的消息数量,并且不显示该变量是否未更改。

Something like:

document.addEventListener(
  "DOMNodeInserted", 
  function() { 
    var count = parseInt(document.getElementById("fb_menu_inbox_unread_count").textContent);
    if (count > 0 && count != lastCount) {
      alert("You have "+count+" new message"+(count==1 ? "" : "s")+"."); }, true);
    }
    lastCount = count;  // Remember count to avoid continuous alerts.

Also, I would avoid writing code all in one like, as you did in your original post. It makes it more difficult to read and change if need be.

另外,我会避免像在原始帖子中那样编写代码。如果需要,它会使阅读和更改变得更加困难。

#2


Set a custom cookie using document.cookie to save the count, then do your regular checks

使用document.cookie设置自定义cookie以保存计数,然后进行常规检查