jQuery+Ajax實(shí)現(xiàn)限制查詢間隔的方法
本文實(shí)例講述了jQuery+Ajax實(shí)現(xiàn)限制查詢間隔的方法。分享給大家供大家參考,具體如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Jquery20150305.aspx.cs" Inherits="Jquery20150305" %>
<!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>Jquery異步查詢加載效果</title>
<script src="JS/jquery-1.9.1.js" type="text/javascript"></script>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.span_query { cursor:pointer;}
</style>
<script type="text/javascript">
$(function () {
$(".span_query").click(function () {
var val = $(this).attr("data-value");
var id = $(this).attr("id");
AjaxQuery($(this),val);
});
});
function AjaxQuery(obj, v) {
$.ajax({
url: 'Ajax/Handler.ashx?queryType=score&queryValue=' + v,
type: 'POST',
dataType: 'text',
timeout: 10000,
cache: false,
beforeSend: LoadFunction,
error: erryFunction,
success: succFunction
})
function LoadFunction() {
obj.html('<img src="Images/loading02.gif" />');
}
function erryFunction() {
obj.html('error');
}
function succFunction(tt) {
obj.html('');
obj.html(tt);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%" class="gvCss">
<tr class="head"><td style="width:10%;">姓名</td><td style="width:30%;">語文</td><td style="width:30%;">數(shù)學(xué)</td><td style="width:30%;">英語</td></tr>
<tr><td>張三</td>
<td id="query1" title="點(diǎn)擊查詢" class="span_query" data-value="1">查詢</td>
<td id="query2" title="點(diǎn)擊查詢" class="span_query" data-value="2">查詢</td>
<td id="query3" title="點(diǎn)擊查詢" class="span_query" data-value="3">查詢</td></tr>
</table>
</div>
</form>
</body>
</html>
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.SessionState;
//Handler.ashx
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string queryType = context.Request["queryType"];
string queryValue = context.Request["queryValue"];
if (context.Session["preQuery"] == null) //第一次查詢
{
context.Session["preQuery"] = queryValue + "@" + DateTime.Now.AddDays(-1);
context.Session["currQuery"] = queryValue + "@" + DateTime.Now;
}
else //存在上次查詢
{
string[] preStrs = context.Session["currQuery"].ToString().Split('@');
context.Session["preQuery"] = queryValue + "@" + preStrs[1]; //重置為當(dāng)前查詢參數(shù)+上次查詢時(shí)間
context.Session["currQuery"] = queryValue + "@" + DateTime.Now;
}
string[] strs=context.Session["preQuery"].ToString().Split('@');
if (strs[0] == queryValue) //同一請(qǐng)求限制查詢間隔
{
DateTime preTime = Convert.ToDateTime(strs[1]);
DateTime nowTime = DateTime.Now;
bool flag = CheckQueryTimeSpan(preTime, nowTime, 3);
if (flag)
{
context.Response.Write("查詢間隔3秒");
}
else
{
context.Response.Write("98");
}
}
context.Response.End();
}
/// <summary>
/// 判斷本次查詢和上次查詢間隔是否小于指定秒數(shù)
/// </summary>
/// <param name="preTime">上次查詢時(shí)間</param>
/// <param name="nowTime">本次查詢時(shí)間</param>
/// <param name="timeSpan">指定秒數(shù)</param>
/// <returns></returns>
public bool CheckQueryTimeSpan(DateTime preTime, DateTime nowTime, int timeSpan)
{
TimeSpan ts = nowTime - preTime;
int difference = ts.Seconds;
bool flag = (difference < timeSpan) ? true : false;
return flag;
}
public bool IsReusable {
get {
return false;
}
}
}
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery zTree搜索-關(guān)鍵字查詢 遞歸無限層功能實(shí)現(xiàn)代碼
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁)
- 基于jquery實(shí)現(xiàn)的類似百度搜索的輸入框自動(dòng)完成功能
- 基于jquery的仿百度搜索框效果代碼
- 基于jQuery實(shí)現(xiàn)搜索關(guān)鍵字自動(dòng)匹配功能
- Jquery實(shí)現(xiàn)搜索框提示功能示例代碼
- 基于jQuery實(shí)現(xiàn)頁面搜索功能
- jQuery實(shí)現(xiàn)模糊查詢的方法分析
- 基于jquery的表格排序
- jQuery利用sort對(duì)DOM元素進(jìn)行排序操作
- jQuery實(shí)現(xiàn)條件搜索查詢、實(shí)時(shí)取值及升降序排序的方法分析
相關(guān)文章
Jquery Validate 正則表達(dá)式實(shí)用驗(yàn)證代碼大全
Jquery + Ajax調(diào)用webService實(shí)例代碼(asp.net)
關(guān)于echarts在節(jié)點(diǎn)顯示動(dòng)態(tài)數(shù)據(jù)及添加提示文本所遇到的問題
jQuery中hover與mouseover和mouseout的區(qū)別分析
jQuery基于當(dāng)前元素進(jìn)行下一步的遍歷
解決jquery操作checkbox火狐下第二次無法勾選問題
一款基于jQuery的圖片場(chǎng)景標(biāo)注提示彈窗特效

