I have a site for testing Ajax… and it works:
<%@ 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>
And here is the site it’s laoding:
<%@ 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>
And the codebehind for the above site:
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;
}
}
But when I load the site ajax-Content.aspx it start reloading freakingly fast… so in 30 seconds I can’t read the text it load in the ajax.Content.aspx site.
Any ideas??
xmlHttp.onreadystatechange fires not only on success, you should only start the new timeout on readystate 4
furtherMore you should define a variable for the timeout to be able to clear an existing timeout before running a new.