JQuery實(shí)現(xiàn)簡單的服務(wù)器輪詢效果實(shí)例
本文實(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 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請(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ì)有所幫助。
- jquery與php結(jié)合實(shí)現(xiàn)AJAX長輪詢(LongPoll)
- jQuery結(jié)合AJAX之在頁面滾動(dòng)時(shí)從服務(wù)器加載數(shù)據(jù)
- 模擬jQuery ajax服務(wù)器端與客戶端通信的代碼
- 用Jquery實(shí)現(xiàn)可編輯表格并用AJAX提交到服務(wù)器修改數(shù)據(jù)
- jquery中如何獲得服務(wù)器控件實(shí)現(xiàn)思路
- 使用jquery動(dòng)態(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)文章
form表單只提交數(shù)據(jù)而不進(jìn)行頁面跳轉(zhuǎn)的解決方案
將數(shù)據(jù)提交到saveReport(form的action指向)頁面,但是頁面又不進(jìn)行跳轉(zhuǎn),即保持當(dāng)前頁面不變呢?利用jquery的ajaxSubmit函數(shù)以及form的onsubmit函數(shù)完成2013-09-09
jQuery利用鍵盤上下鍵移動(dòng)表格內(nèi)容
這篇文章主要為大家詳細(xì)介紹了jQuery利用鍵盤上下鍵移動(dòng)表格內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
jQuery EasyUI API 中文文檔 - TimeSpinner時(shí)間微調(diào)器
jQuery EasyUI API 中文文檔 - TimeSpinner時(shí)間微調(diào)器介紹,需要的朋友可以參考下。2011-10-10
jQuery 添加/移除CSS類實(shí)現(xiàn)代碼
在網(wǎng)頁設(shè)計(jì)中,我們常常要使用Javascript來改變頁面元素的樣式。2010-02-02
jQuery實(shí)現(xiàn)判斷上傳圖片類型和大小的方法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)判斷上傳圖片類型和大小的方法,結(jié)合實(shí)例形式分析了jQuery針對(duì)上傳圖片屬性獲取、判定相關(guān)操作技巧,需要的朋友可以參考下2018-04-04

