轮询是在客户端建立一个时钟,每隔一段时间对服务器发送一次请求,获取服务器数据,是实现消息推送最简单最易实现的一种方式,最大的缺点浏览器需要不断的向服务器发出请求,这样会占用很多的带宽,造成资源浪费
下面是JS代码:
1 $(document).ready(function () { 2 setInterval(invoke, 20000); 3 }); 4 function invoke() { 5 var xhr = $.ajax({ 6 type: "POST", 7 dataType: ‘json‘, 8 url: "Sys_msg.get_unRead_count.xhd", 9 data: "", 10 success: function (result) { 11 var obj = eval(result); 12 if (obj > msg_count) 13 { 14 $.ligerTip({ 15 content: "您有<font color=red> " + (obj-msg_count) + "</font> 条新增未读消息,请注意查收! <br> <a href=‘javascript:void(0)‘ onclick=\"window.top.f_addTab(‘Sys_msg‘,‘消息管理‘,‘personal/message/sys_msg.aspx‘)\">进入处理</a>", 16 width: 240, 17 //var winH = $(window).height(), winW = $(window).width(); 18 x: tipW - 260, //设置气泡位置,右下角气泡显示 19 y: tipH - 80, 20 callback: function (t) { 21 var i = 10; //10秒倒计时 22 calctime(t, i); 23 } 24 }); 25 } 26 msg_count = obj; 27 28 } 29 }); 30 } 31 var tipH = $(window).height(), tipW = $(window).width(); //获取屏幕尺寸 32 function calctime(t, i) //这个函数用了递归来实现倒计时 33 { 34 i--; 35 (i >= 0) 36 ? setTimeout(function () { 37 calctime(t, i); 38 }, 1000) 39 : $(t).remove(); 40 }
后台代码:
1 public class Sys_msg 2 { 3 public static BLL.Sys_msg msg = new BLL.Sys_msg(); 4 public static Model.Sys_msg model = new Model.Sys_msg(); 5 6 public HttpContext Context; 7 public string emp_id; 8 public string emp_name; 9 public HttpRequest request; 10 public string uid; 11 12 13 public Sys_msg() 14 { 15 } 16 17 public Sys_msg(HttpContext context) 18 { 19 Context = context; 20 request = context.Request; 21 22 var userinfo = new User_info(); 23 employee = userinfo.GetCurrentEmpInfo(context); 24 25 emp_id = employee.id; 26 emp_name = PageValidate.InputText(employee.name, 50); 27 uid = PageValidate.InputText(employee.uid, 50); 28 29 } 30 31 32 public int get_unRead_count() 33 { 34 string serchtext = $" 1=1 "; 35 //int rowcount = 0; 36 serchtext += " and isRead=0"; 37 serchtext += " and UserID like N‘%" + emp_id + "%‘"; 38 DataSet ds = msg.GetList(serchtext); 39 //rowcount = ds.Tables[0].Rows.Count; 40 return ds.Tables[0].Rows.Count; 41 } 42 43 }
原文地址:https://www.cnblogs.com/Alvis-Lv/p/11130309.html
时间: 2024-10-12 12:07:23