本文實(shí)例講述了JQuery實(shí)現(xiàn)簡單的服務(wù)器輪詢效果。分享給大家供大家參考,具體如下:
很多論壇都有進(jìn)入后,彈出提示,說有多少封郵件沒有看,或者是一個(gè)oa系統(tǒng),進(jìn)入后,提示有多少個(gè)任務(wù)沒有做。每隔一段時(shí)間會(huì)提示一次,但是如何實(shí)現(xiàn)呢。其實(shí),利用jquery的話,會(huì)比較簡單,核心元素就是json格式解析和setInterval()函數(shù)。下面一起來實(shí)現(xiàn):
首先,我們default.aspx的頁面如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>服務(wù)器輪詢</title> <link rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" /> <script src="http://www.gimoo.net/t/1903/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="http://www.gimoo.net/t/1903/js/i18n/grid.locale-en.js" type="text/javascript"></script> <script src="http://www.gimoo.net/t/1903/js/jquery.jqGrid.min.js" type="text/javascript"></script> <script src="http://www.gimoo.net/t/1903/js/src/grid.base.js" type="text/javascript"></script> <script type="text/javascript"> function showUnreadNews() { $(document).ready(function() { $.ajax({ type: "GET", url: "Result.ashx", dataType: "json", success: function(msg) { //alert(msg); $.each(msg, function(id, title) { $("#news").append("<a href=detailnews.aspx?id=" + id + ">" + title + "</a><br>"); }); } }); }); } setInterval('showUnreadNews()',5000); </script> </head> <body> <form id="form1" runat="server"> <div id="news"> </div> </form> </body> </html>
上面代碼主要利用ajax函數(shù)向Result.ashx頁面發(fā)出ajax請(qǐng)求,然后由Result.ashx這個(gè)頁面返回json數(shù)據(jù),并進(jìn)行解析,最后利用setInterval()函數(shù)實(shí)現(xiàn)輪詢效果,具體的Result.ashx頁面代碼如下:
<%@ WebHandler Language="C#" Class="Result" %> using System; using System.Web; using System.Text; using System.Data.SQLite; using System.Data; public class Result : IHttpHandler { public void ProcessRequest (HttpContext context) { string sql = "select * from Content where NewsFlag=0"; DataTable dt = new DataTable(); using (SQLiteConnection conn = new SQLiteConnection(InitSQLite().Connection)) { SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, conn); sda.Fill(dt); } string jsonStr = GetJson(dt); context.Response.ContentType = "text/json"; context.Response.Buffer = true; context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1); context.Response.AddHeader("pragma", "no-cache"); context.Response.AddHeader("cache-control", ""); context.Response.CacheControl = "no-cache"; context.Response.Write(jsonStr); } public static string GetJson(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{"); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append( dt.Rows[i]["NewsID"].ToString() + ":" +"""+ dt.Rows[i]["NewsTitle"].ToString()+"","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } public Sqlite InitSQLite() { Sqlite _sqLite = new Sqlite(); _sqLite.ConnetStringBuilder.DataSource = AppDomain.CurrentDomain.BaseDirectory + "News.db3"; _sqLite.ConnetStringBuilder.Pooling = true; _sqLite.ConnetStringBuilder.FailIfMissing = true; _sqLite.ConnetStringBuilder.UseUTF16Encoding = true; return _sqLite; } public bool IsReusable { get { return false; } } }
數(shù)據(jù)庫使用的是sqlite,具體使用方式請(qǐng)自查。這個(gè)處理文件中,最重要的是由datatable數(shù)據(jù)生成符合格式的json數(shù)據(jù)。
這樣,系統(tǒng)最終就實(shí)現(xiàn)了,每隔5S鐘,首頁會(huì)向服務(wù)器輪詢一次數(shù)據(jù),以便獲得最新的數(shù)據(jù)。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery切換特效與技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。