JQuery實(shí)現(xiàn)簡單的服務(wù)器輪詢效果實(shí)例
本文實(shí)例講述了JQuery實(shí)現(xiàn)簡單的服務(wù)器輪詢效果。分享給大家供大家參考,具體如下:
很多論壇都有進(jìn)入后,彈出提示,說有多少封郵件沒有看,或者是一個oa系統(tǒng),進(jìn)入后,提示有多少個任務(wù)沒有做。每隔一段時間會提示一次,但是如何實(shí)現(xiàn)呢。其實(shí),利用jquery的話,會比較簡單,核心元素就是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 href="Content/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" /> <link href="Content/ui.jqgrid.css" rel="stylesheet" type="text/css" /> <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script> <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script> <script src="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請求,然后由Result.ashx這個頁面返回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,具體使用方式請自查。這個處理文件中,最重要的是由datatable數(shù)據(jù)生成符合格式的json數(shù)據(jù)。
這樣,系統(tǒng)最終就實(shí)現(xiàn)了,每隔5S鐘,首頁會向服務(wù)器輪詢一次數(shù)據(jù),以便獲得最新的數(shù)據(jù)。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery切換特效與技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
- jquery與php結(jié)合實(shí)現(xiàn)AJAX長輪詢(LongPoll)
- jQuery結(jié)合AJAX之在頁面滾動時從服務(wù)器加載數(shù)據(jù)
- 模擬jQuery ajax服務(wù)器端與客戶端通信的代碼
- 用Jquery實(shí)現(xiàn)可編輯表格并用AJAX提交到服務(wù)器修改數(shù)據(jù)
- jquery中如何獲得服務(wù)器控件實(shí)現(xiàn)思路
- 使用jquery動態(tài)加載javascript以減少服務(wù)器壓力
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值
- Jquery 獲得服務(wù)器控件值的方法小結(jié)
- 基于jquery實(shí)現(xiàn)的服務(wù)器驗(yàn)證控件的啟用和禁用代碼
- jQuery生成asp.net服務(wù)器控件的代碼
相關(guān)文章
jquery顯示loading圖片直到網(wǎng)頁加載完成的方法
這篇文章主要介紹了jquery顯示loading圖片直到網(wǎng)頁加載完成的方法,涉及jquery頁面加載與動畫效果的使用技巧,需要的朋友可以參考下2015-06-06Jquery遍歷select option和添加移除option的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫query遍歷select option和添加移除option的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08jQuery Validate表單驗(yàn)證入門學(xué)習(xí)
這篇文章主要介紹了jQuery Validate表單驗(yàn)證入門知識,該插件捆綁了一套有用的驗(yàn)證方法,包括 URL 和電子郵件驗(yàn)證,同時提供了一個用來編寫用戶自定義方法的 API,感興趣的小伙伴們可以參考一下2015-12-12jQuery基于擴(kuò)展簡單實(shí)現(xiàn)倒計(jì)時功能的方法
這篇文章主要介紹了jQuery基于擴(kuò)展簡單實(shí)現(xiàn)倒計(jì)時功能的方法,涉及jQuery擴(kuò)展與回調(diào)函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2016-05-05jquery實(shí)現(xiàn)點(diǎn)擊左右按鈕切換圖片
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)點(diǎn)擊左右按鈕切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01表單類各種類型(文本框)失去焦點(diǎn)效果jquery代碼
基于jquery實(shí)現(xiàn)表單類各種類型(文本框)失去焦點(diǎn)效果,代碼簡單實(shí)用,感興趣的朋友可以參考下,希望對你有所幫助2013-04-04Jquery中國地圖熱點(diǎn)效果-鼠標(biāo)經(jīng)過彈出提示層信息的簡單實(shí)例
本篇文章主要是對Jquery中國地圖熱點(diǎn)效果-鼠標(biāo)經(jīng)過彈出提示層信息的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02