jQuery+Ajax实现限制查询间隔的方法
本文实例讲述了jQuery+Ajax实现限制查询间隔的方法。分享给大家供大家参考,具体如下:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Jquery20150305.aspx.cs"Inherits="Jquery20150305"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Jquery异步查询加载效果</title>
<scriptsrc="JS/jquery-1.9.1.js"type="text/javascript"></script>
<linkhref="Styles/Site.css"rel="stylesheet"type="text/css"/>
<styletype="text/css">
.span_query{cursor:pointer;}
</style>
<scripttype="text/javascript">
$(function(){
$(".span_query").click(function(){
varval=$(this).attr("data-value");
varid=$(this).attr("id");
AjaxQuery($(this),val);
});
});
functionAjaxQuery(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
})
functionLoadFunction(){
obj.html('<imgsrc="Images/loading02.gif"/>');
}
functionerryFunction(){
obj.html('error');
}
functionsuccFunction(tt){
obj.html('');
obj.html(tt);
}
}
</script>
</head>
<body>
<formid="form1"runat="server">
<div>
<tablestyle="width:100%"class="gvCss">
<trclass="head"><tdstyle="width:10%;">姓名</td><tdstyle="width:30%;">语文</td><tdstyle="width:30%;">数学</td><tdstyle="width:30%;">英语</td></tr>
<tr><td>张三</td>
<tdid="query1"title="点击查询"class="span_query"data-value="1">查询</td>
<tdid="query2"title="点击查询"class="span_query"data-value="2">查询</td>
<tdid="query3"title="点击查询"class="span_query"data-value="3">查询</td></tr>
</table>
</div>
</form>
</body>
</html>
<%@WebHandlerLanguage="C#"Class="Handler"%>
usingSystem;
usingSystem.Web;
usingSystem.Web.SessionState;
//Handler.ashx
publicclassHandler:IHttpHandler,IRequiresSessionState
{
publicvoidProcessRequest(HttpContextcontext)
{
context.Response.ContentType="text/plain";
stringqueryType=context.Request["queryType"];
stringqueryValue=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];//重置为当前查询参数+上次查询时间
context.Session["currQuery"]=queryValue+"@"+DateTime.Now;
}
string[]strs=context.Session["preQuery"].ToString().Split('@');
if(strs[0]==queryValue)//同一请求限制查询间隔
{
DateTimepreTime=Convert.ToDateTime(strs[1]);
DateTimenowTime=DateTime.Now;
boolflag=CheckQueryTimeSpan(preTime,nowTime,3);
if(flag)
{
context.Response.Write("查询间隔3秒");
}
else
{
context.Response.Write("98");
}
}
context.Response.End();
}
///<summary>
///判断本次查询和上次查询间隔是否小于指定秒数
///</summary>
///<paramname="preTime">上次查询时间</param>
///<paramname="nowTime">本次查询时间</param>
///<paramname="timeSpan">指定秒数</param>
///<returns></returns>
publicboolCheckQueryTimeSpan(DateTimepreTime,DateTimenowTime,inttimeSpan)
{
TimeSpants=nowTime-preTime;
intdifference=ts.Seconds;
boolflag=(difference<timeSpan)?true:false;
returnflag;
}
publicboolIsReusable{
get{
returnfalse;
}
}
}
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。