使用 jQuery 的动态通知

发布于 2024-08-28 05:55:46 字数 1854 浏览 6 评论 0原文

我目前正在为我们的员工构建一个主页,该主页将设置为他们的浏览器中的主页。我目前正在建立一个设施,指定的工作人员可以通过该设施向所有工作人员发送通知。这些将弹出在他们的主页上,而不需要他们刷新页面。

我目前得到了下面的代码,该代码对于 1 个通知运行良好,但一次可能有多个通知等待显示。我已切换为输出 json,但我不确定如何修改我的代码来使用它。

我正在使用PeriodicalUpdater 和jGrowl 插件来获得此功能,如果有更好的替代方案,请随时提出建议。

    $.PeriodicalUpdater({
        url: 'getNotifications.aspx',
        maxTimeout: 6000,
        type: 'json'
    },
    function(data) {
        var message = data;
        if (message != '') {
            $.jGrowl(message, { sticky: true });
        }
    });

作为一项附加功能,当用户关闭通知时是否可以将其存储在 cookie 中,这样他们就不会再看到它了?

谢谢。


好的,我已经更新了代码,现在是这样了;

        $(document).ready(function() {
        var alreadyGrowled = new Array();

        var growlCheckNew = function() {
            $.getJSON('getNotifications.aspx', function(data) {
                $(data).each(function(entryIndex,entry) {
                    var newMessage = true;
                    $(alreadyGrowled).each(function(index,msg_id) {
                        if (entry['ID'] == msg_id) {
                            newMessage = false;
                        }
                    });

                    if (newMessage == true) {
                        $.jGrowl(entry['Message'], {
                            sticky: true,
                            header: entry['Title'],
                            beforeClose: function(e,m) {
                                alert('About to close this message');
                            }
                        });
                    }
                    alreadyGrowled.push(entry['ID']);
                });
            });
        }

        growlCheckNew();

        var msgTimer = setInterval(growlCheckNew, 1000);

    });

但是,当我关闭咆哮通知时,我的警报没有触发?!在将一些代码保存到 cookie 之前,我已经将它放在那里来检查它是否有效。这看起来合适吗?

I'm currently building a homepage for our staff which is to be set as the homepage in their browser. I'm currently building the facility whereby nominated members of staff can send notifications to all staff. These will popup on their home page without the need for them to refresh the page.

I've currently got the code below which worked fine for 1 notification, but there may be more than 1 at a time waiting to be shown. I've switched to outputting json but I'm not sure how to modify my code to consume it.

I'm using the PeriodicalUpdater and jGrowl plugins to get this functionality, if there's better alternatives then feel free to sugget them.

    $.PeriodicalUpdater({
        url: 'getNotifications.aspx',
        maxTimeout: 6000,
        type: 'json'
    },
    function(data) {
        var message = data;
        if (message != '') {
            $.jGrowl(message, { sticky: true });
        }
    });

As an additional piece of functionality, would it be possible to store in a cookie when a user has closed a notification so they don't see it again?

Thanks.


OK, I've updated the code so that it's now;

        $(document).ready(function() {
        var alreadyGrowled = new Array();

        var growlCheckNew = function() {
            $.getJSON('getNotifications.aspx', function(data) {
                $(data).each(function(entryIndex,entry) {
                    var newMessage = true;
                    $(alreadyGrowled).each(function(index,msg_id) {
                        if (entry['ID'] == msg_id) {
                            newMessage = false;
                        }
                    });

                    if (newMessage == true) {
                        $.jGrowl(entry['Message'], {
                            sticky: true,
                            header: entry['Title'],
                            beforeClose: function(e,m) {
                                alert('About to close this message');
                            }
                        });
                    }
                    alreadyGrowled.push(entry['ID']);
                });
            });
        }

        growlCheckNew();

        var msgTimer = setInterval(growlCheckNew, 1000);

    });

However, my alerts not firing when I close a growl notification?! I've got that in there to check it works before putting some code in to save it to a cookie. Does this look about right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2024-09-04 05:55:46

您可以将所有消息作为 JSON 输出中的列表输出,并简单地在 javascript 中迭代消息列表。

JSON 输出:

{
    messages:['msg1', 'msg2', 'msg3']
}

JavaScript:

$.PeriodicalUpdater({
    url: 'getNotifications.aspx',
    maxTimeout: 6000,
    type: 'json'
},
function(data) {
    for(message in data.messages) {
        $.jGrowl(message, { sticky: true });
    }
});

you can output all the messages as a list in the JSON output and simply iterate over the message list in javascript.

JSON output:

{
    messages:['msg1', 'msg2', 'msg3']
}

javascript:

$.PeriodicalUpdater({
    url: 'getNotifications.aspx',
    maxTimeout: 6000,
    type: 'json'
},
function(data) {
    for(message in data.messages) {
        $.jGrowl(message, { sticky: true });
    }
});
南冥有猫 2024-09-04 05:55:46

至于cookie问题,您可以使用jQuery Cookie插件来解决那。

句法:

// To set a cookie ‘foo’ with value 'bar':
$.cookie('foo', 'bar');
// To get the value stored in a cookie:
$.cookie('foo'); // 'bar'

As for the cookie problem, you could use the jQuery Cookie plugin for that.

Syntax:

// To set a cookie ‘foo’ with value 'bar':
$.cookie('foo', 'bar');
// To get the value stored in a cookie:
$.cookie('foo'); // 'bar'
柠栀 2024-09-04 05:55:46

在类似的情况下,我们使用在 IIS 后端运行的 rss 文件以及免费工具来编辑 .xml。然后我们使用 RSS 小部件来显示最新新闻,有些人使用 RSS 阅读器,其他人也使用 Outlook。

快速又简单。

In a similar situation we used a rss file running on an IIS backend with a free tool to edit the .xml. We then used an rss widget to display the latest news, some people used an rss reader and others used Outlook as well.

Quick and simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文