Ajax调用开始加载时就像一个怪胎

本文关键字:一个 开始 调用 加载 Ajax | 更新日期: 2023-09-27 18:25:48

我有一个测试Ajax的站点。。。它起作用:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="AJAX.aspx.cs" Inherits="HB___test.AJAX" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function Ajax() {
            var xmlHttp;
            try {
                xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
                } catch (e) {
                    try {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                        alert("No AJAX!?");
                        return false;
                    }
                }
            }
            xmlHttp.onreadystatechange = function () {
                document.getElementById('chat').innerHTML = xmlHttp.responseText;
                setTimeout('Ajax()', 10000);
            }
            xmlHttp.open("GET", "ajax-Content.aspx", true);
            xmlHttp.send(null);
        }
        window.onload = function () {
            setTimeout('Ajax()', 10000);
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    Kummefryser...!
    <div id="chat" class="fisk" style="width: 500px; height: 500px">
    </div>
</asp:Content>

这是它的老网站:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ajax-Content.aspx.cs" Inherits="HB___test.ajax_Content" %>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="lbChat" runat="server" Rows="10" Width="400px"></asp:ListBox>
    </div>
    </form>
</body>

上述站点的代码隐藏:

public partial class ajax_Content : System.Web.UI.Page
    {
        grjenie31Entities gr;
        protected void Page_Load(object sender, EventArgs e)
        {
            gr = new grjenie31Entities();
            var query = from es in gr.chats
                        where es.id > ((from esh in gr.chats select esh.id).Max() - 15)
                        orderby es.timestamps descending
                        select es;
            List<chat> list = new List<chat>();
            foreach (chat chat in query)
            {
                list.Add(chat);
            }
            for (int i = 0; i < list.Count; i++)
            {
                lbChat.Items.Add("[" + list[i].timestamps + "] " + list[i].personID.ToString() + ": " + list[i].besked);
            }
            this.lbChat.SelectedIndex = this.lbChat.Items.Count - 1;
        }
    }

但当我加载网站ajax-Content.aspx时,它开始以惊人的速度重新加载。。。所以在30秒内我无法读取它在ajax中加载的文本。Content.aspx网站。有什么想法吗??

Ajax调用开始加载时就像一个怪胎

xmlHttp.onreadystatechange不仅在成功时触发,而且应该只在readystate4 上启动新的超时

此外,您应该为超时定义一个变量,以便能够在运行新的超时之前清除现有的超时。

相关文章: