Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:13:21+00:00 2026-05-15T06:13:21+00:00

Alright, I’m trying to make an AJAX Chat system that polls the chat database

  • 0

Alright, I’m trying to make an AJAX Chat system that polls the chat database every 400ms. That part is working, the part of which isn’t is the Active User List. When I try to combine the two requests, the first two requests are made, then the whole thing snowballs and the usually timed (12 second) Active User List request starts updating every 1ms and the first request NEVER happens again. Displayed is the entire AJAX code for both requests:

var waittime=400;chatmsg=document.getElementById("chatmsg");
room = document.getElementById("roomid").value; 
chatmsg.focus()
document.getElementById("chatwindow").innerHTML = "loading...";
document.getElementById("userwindow").innerHTML = "Loading User List...";
var xmlhttp = false;
var xmlhttp2 = false;
var xmlhttp3 = false;
function ajax_read(url) {
if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    if(xmlhttp.overrideMimeType){
        xmlhttp.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
    document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;
    setTimeout("ajax_read('methods.php?method=r&room=" + room +"')", waittime);
    }
}
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
}
function user_read(url) {
if(window.XMLHttpRequest){
    xmlhttp3=new XMLHttpRequest();
    if(xmlhttp3.overrideMimeType){
        xmlhttp3.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp3) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState==4) {
    document.getElementById("userwindow").innerHTML = xmlhttp3.responseText;
    setTimeout("ajax_read('methods.php?method=u&room=" + room +"')", 12000);
    }
}
xmlhttp3.open('GET',url,true);
xmlhttp3.send(null);
}
function ajax_write(url){
if(window.XMLHttpRequest){
    xmlhttp2=new XMLHttpRequest();
    if(xmlhttp2.overrideMimeType){
        xmlhttp2.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp2) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp2.open('GET',url,true);
xmlhttp2.send(null);
}
function submit_msg(){
nick = document.getElementById("chatnick").value;
msg = document.getElementById("chatmsg").value;
document.getElementById("chatmsg").value = "";
ajax_write("methods.php?method=w&m=" + msg + "&n=" + nick + "&room=" + room + "");
}
function keyup(arg1) { 
if (arg1 == 13) submit_msg(); 
}
var intUpdate = setTimeout("ajax_read('methods.php')", waittime);
var intUpdate = setTimeout("user_read('methods.php')", waittime);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:13:22+00:00Added an answer on May 15, 2026 at 6:13 am

    The problem is that in user_read a timer is set up that runs ajax_read after 12 s, with the correct URL. So, when this ajax_read is called, it fetches information and sets up a new timeout, this time calling ajax_read after waittime, with ?method=r…. So after the first timeout of user_read, it is never called again.

    FYI, I watched this with (the Net panel of) Firebug and a bogus form and methods.php on a local web server. It became clear after setting waittime to 4000 and using .innerHTML += …, resulting in two calls every 4 seconds.

    index.html (I know, it’s quick’n’dirty):

    <!DOCTYPE html>
    <html>
     <head>
      <meta charset=UTF-8>
      <title>Chat</title>
     </head>
     <body>
      <input id="chatnick" type="text" value="Nickname"><br>
      <input id="roomid" type="text" value="4"><br>
      <input id="chatmsg" type="text"><br>
      <div id="userwindow" style="width: 500px; height: 300px"></div><br>
      <div id="chatwindow" style="width: 300px; height: 300px"></div><br>
      <script src="js.js"></script>
     </body>
    </html>
    

    bogus methods.php:

    blah<br>
    

    Also be aware of the possibility that xmlhttp.status might not be 200.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm trying to understand follow sets and I think I got it except
Alright, I'm just trying to learn about using Contact information, but I'm a bit
Alright, I have a div which is 50px (height), and the width does not
Alright I'm going to create a fairly complex form to post via AJAX a
Alright, I'm trying to pass a Dictionary, where key = int, and value =
Alright, I don't know if anyone has tried to do this yet, however. I
Alright I want to create a .csv file in C#. I have been looking
Alright I'm a bit baffled by this one. Changing an unrelated int property on
Alright, I guess this question has been asked a lot of times here. I
Alright, Im going about this, with what is probably a really complicated solution, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.