Method of Restricting Query Interval by jQuery+Ajax

  • 2021-06-28 06:16:28
  • OfStack

In this paper, an example is given to describe the method of limiting query interval by jQuery+Ajax. Share it for your reference, as follows:


<%@ 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 Asynchronous query loading effect 
 </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%;"> Name 
 </td><td style="width:30%;"> Language 
 </td><td style="width:30%;"> Mathematics 
 </td><td style="width:30%;"> English 
 </td></tr>
    <tr><td> Zhang 
 3</td>
      <td id="query1" title=" Click Query 
 " class="span_query" data-value="1"> Query 
 </td>
      <td id="query2" title=" Click Query 
 " class="span_query" data-value="2"> Query 
 </td>
      <td id="query3" title=" Click Query 
 " class="span_query" data-value="3"> Query 
 </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) // No. 1 
 1 Secondary query 
 
    {
      context.Session["preQuery"] = queryValue + "@" + DateTime.Now.AddDays(-1);
      context.Session["currQuery"] = queryValue + "@" + DateTime.Now;
    }
    else // Last query exists 
 
    {
      string[] preStrs = context.Session["currQuery"].ToString().Split('@');
      context.Session["preQuery"] = queryValue + "@" + preStrs[1]; // Reset to current query parameter 
 + Last query time 
 
      context.Session["currQuery"] = queryValue + "@" + DateTime.Now;
    }
    string[] strs=context.Session["preQuery"].ToString().Split('@');
    if (strs[0] == queryValue) // Same as 
 1 Request to limit query interval 
 
    {
      DateTime preTime = Convert.ToDateTime(strs[1]);
      DateTime nowTime = DateTime.Now;
      bool flag = CheckQueryTimeSpan(preTime, nowTime, 3);
      if (flag)
      {
        context.Response.Write(" Query interval 
 3 Seconds 
 ");
      }
      else
      {
        context.Response.Write("98");
      }
    }
    context.Response.End();
  }
  /// <summary>
  ///  Determines whether the interval between this query and the last query is less than the specified number of seconds 
 
  /// </summary>
  /// <param name="preTime"> Last query time 
 </param>
  /// <param name="nowTime"> Time of this inquiry 
 </param>
  /// <param name="timeSpan"> Specify the number of seconds 
 </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;
    }
  }
}

For more readers interested in jQuery related content, please check the topics of this site: "Summary of Ajax Usage in jquery", "Summary of jQuery Table (table) Operation Skills", "Summary of jQuery Drag Effects and Skills", "Summary of jQuery Extension Skills", "Summary of jQuery Common Classic Effects", "Summary of jQuery Animation and Special Effects Usage", "Summary of jquery Selector Usage" and "Summary of jQuery Common Plugins and Usage"

I hope this article is helpful to everyone's jQuery programming.


Related articles: