JQuery implements simple server round-robin For instances. jquery round-robin
This article describes how JQuery implements simple server round robin. We will share this with you for your reference. The details are as follows:
When many forums are accessed, a prompt is displayed, indicating how many emails are not viewed, or an OA system, indicating how many tasks are not performed. A prompt is displayed at intervals, but how can this problem be implemented. In fact, using jquery is relatively simple. The core element is the json format parsing and setInterval () functions. The following is an implementation:
First, the default. aspx page is shown as follows:
<% @ 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">
The above code mainly uses ajax functions to return results. the ashx page sends an ajax request and. on the ashx page, json data is returned and parsed. Finally, the setInterval () function is used to implement the round-robin effect. The specific Result is returned. the ashx Page code is as follows:
<%@ 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; } }}
The database uses sqlite. Check the usage. In this processing file, the most important thing is that the datatable data generates formatted json data.
In this way, the system will eventually implement. Every 5S, the homepage will round-robin data to the server to obtain the latest data.