javascript - Stackoverflow、Linkedin 等有新事件时的通知

标签 javascript jquery asp.net-mvc notifications signalr

许多网站在有新帖子时都有通知系统。

例如,

stackoverflow enter image description here

LinkedIn enter image description here

单击此按钮后,页面将重新加载。

我想在我的应用程序中实现这种系统。 因此,当数据库中添加新项目时,以及当用户单击它时,显示此类通知。重新加载页面。

有人知道这种通知是如何工作的以及我们如何实现它吗? 任何与此相关的帖子或想法都会有所帮助。

此外,如果有人认为这个问题可以有更多标签,请随时添加。

最佳答案

以下是如何执行此操作的简单想法。

客户端自动刷新并使用ajax post获取内容。

<h2>Notification</h2>
<div id="divNotif" style="display:none;"></div>

<script id="sessionJs" type="text/javascript">
    function GetNotification() {
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetNotification", "Notification")',
                dataType: "html",
                success: function (data) {
                    $("#divNotif").html(data);
                    $("#divNotif").fadeIn('slow');

                    setTimeout(function () { GetNotification(); }, 5000); //change timeout duration here
                },
                error: function (data) {
                    //alert("BAD:" + data.statusText);
                }
       });
    }

   GetNotification();
</script>

Controller 返回json

    [HttpPost]
    public ActionResult GetNotification()
    {
        int notifCount = fromDb.NotificationCount;
        return Content("<span>New notification " + notifCount + "</span>", "text/html"); // returning a PartialView would be better one
    }

关于javascript - Stackoverflow、Linkedin 等有新事件时的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22527753/

相关文章:

用于网站注释/游览的 jquery 插件?

html - 显示设置为内联 block 的元素在 MVC for 循环中不起作用

javascript - 在javascript中将字符aabbbcccc++压缩为a@2b@3c@4+@3

javascript - 如何旋转 Canvas 中的元素

jquery - 如何在jquery上更改鼠标悬停时的链接图像

c# - ASP.NET MVC5 路由到 View 上的 div

c# - 时间选择器保存 12 小时至 24 小时格式

javascript - 使用字符串中的名称和参数调用 JS 函数

javascript - 返回 $http 中的父函数

javascript - 在页面内包含 API 调用以延迟加载