jQuery+Ajax实现限制查询间隔的方法

所属分类: 网络编程 / JavaScript 阅读数: 1047
收藏 0 赞 0 分享

本文实例讲述了jQuery+Ajax实现限制查询间隔的方法。分享给大家供大家参考,具体如下:

<%@ 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%;">数学</td><td style="width:30%;">英语</td></tr>
    <tr><td>张三</td>
      <td id="query1" title="点击查询" class="span_query" data-value="1">查询</td>
      <td id="query2" title="点击查询" class="span_query" data-value="2">查询</td>
      <td id="query3" title="点击查询" 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]; //重置为当前查询参数+上次查询时间
      context.Session["currQuery"] = queryValue + "@" + DateTime.Now;
    }
    string[] strs=context.Session["preQuery"].ToString().Split('@');
    if (strs[0] == queryValue) //同一请求限制查询间隔
    {
      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>
  /// 判断本次查询和上次查询间隔是否小于指定秒数
  /// </summary>
  /// <param name="preTime">上次查询时间</param>
  /// <param name="nowTime">本次查询时间</param>
  /// <param name="timeSpan">指定秒数</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;
    }
  }
}

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结

希望本文所述对大家jQuery程序设计有所帮助。

更多精彩内容其他人还在看

Canvas实现放射线动画效果

本文主要分享了Canvas实现放射线动画的示例代码。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

微信小程序 image组件binderror使用例子与js中的onerror区别

这篇文章主要介绍了微信小程序 image组件binderror使用例子与js中的onerror区别的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

原生js轮播(仿慕课网)

本文主要分享了原生js实现仿慕课网的轮播效果。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

Bootstrap table简单使用总结

这篇文章主要为大家总结了Bootstrap table的简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

微信小程序之MaterialDesign--input组件详解

本篇文章主要介绍了微信小程序之MaterialDesign--input组件详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析javaScript中的浅拷贝和深拷贝

本篇文章主要介绍了浅析javaScript中的浅拷贝和深拷贝,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

js时间戳和c#时间戳互转方法(推荐)

下面小编就为大家带来一篇js时间戳和c#时间戳互转方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Bootstrap模态框使用详解

这篇文章主要为大家详细介绍了Bootstrap模态框的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Jil,高效的json序列化和反序列化库

下面小编就为大家带来一篇Jil,高效的json序列化和反序列化库。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

BootStrap实现带关闭按钮功能

这篇文章主要介绍了BootStrap实现带关闭按钮功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多