本文實(shí)例講述了JQuery實(shí)現(xiàn)簡(jiǎn)單的服務(wù)器輪詢效果。分享給大家供大家參考,具體如下:
很多論壇都有進(jìn)入后,彈出提示,說(shuō)有多少封郵件沒(méi)有看,或者是一個(gè)oa系統(tǒng),進(jìn)入后,提示有多少個(gè)任務(wù)沒(méi)有做。每隔一段時(shí)間會(huì)提示一次,但是如何實(shí)現(xiàn)呢。其實(shí),利用jquery的話,會(huì)比較簡(jiǎn)單,核心元素就是json格式解析和setInterval()函數(shù)。下面一起來(lái)實(shí)現(xiàn):
首先,我們default.aspx的頁(yè)面如下所示:
<%@ 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頁(yè)面發(fā)出ajax請(qǐng)求,然后由Result.ashx這個(gè)頁(yè)面返回json數(shù)據(jù),并進(jìn)行解析,最后利用setInterval()函數(shù)實(shí)現(xiàn)輪詢效果,具體的Result.ashx頁(yè)面代碼如下:
<%@ 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ù)庫(kù)使用的是sqlite,具體使用方式請(qǐng)自查。這個(gè)處理文件中,最重要的是由datatable數(shù)據(jù)生成符合格式的json數(shù)據(jù)。
這樣,系統(tǒng)最終就實(shí)現(xiàn)了,每隔5S鐘,首頁(yè)會(huì)向服務(wù)器輪詢一次數(shù)據(jù),以便獲得最新的數(shù)據(jù)。
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。