按OK后如何停止javascript警报显示

编程入门 行业动态 更新时间:2024-10-28 02:31:07
本文介绍了按OK后如何停止javascript警报显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我的Facebook收件箱中有东西,我想显示一个提醒。我认为这可以很容易地使用用户脚本完成...这是我迄今为止(感谢 userscripts 论坛):

document.addEventListener(DOMNodeInserted,function(){ var count; if((count = parseInt(document.getElementById(fb_menu_inbox_unread_count)。textContent))> 0) alert(你有+ count +新消息+(count == 1? :s)+。); },true);

这个功能很好,除了在点击确定之后,消息卡在循环中,并保持弹出向上。点击关闭警报后是否有办法停止讯息?

解决方案

添加一个跟踪多少条消息的变量有最后一次警报,如果该变量没有改变,则不显示。

某些东西:

document.addEventListener(DOMNodeInserted, function(){ var count = parseInt(document.getElementById(fb_menu_inbox_unread_count).textContent); if(count> 0&& count!= lastCount){ alert(你有+ count +新消息+(count == 1?:s) +。);},true); } lastCount = count; //记住计数以避免连续警报。

此外,我将避免像原始帖子一样编写所有代码。如果需要,它会更难读取和更改。

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):

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?

解决方案

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.

更多推荐

按OK后如何停止javascript警报显示

本文发布于:2023-11-27 02:03:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!