Jump to content

[SOLVED] Ajax chatroom


jaymc

Recommended Posts

I have an ajax chatroom that works with a request to the database to pull in new messages, then append it to the bottom of the contents of a div

 

This works but as you can imagin after 10 minutes of being in the chat the chat messages build up and eventually start to make it go a bit slow and in most cases crash

 

How can I overcome this, at the moment new messages are added pretty much like this

 

Document.getElementById(‘messages’).innerHTML = Document.getElementById(‘messages’).innerHTML + new_messages

 

Hope someone can help

Link to comment
Share on other sites

I cant really erase all content as that will look like there has been an error or something

 

is there a way to erase everything apart from the last 10 lines or something? Thus clearing the redundant clutter above that has been read?

Link to comment
Share on other sites

try this (it's not perfect; but it's pretty freak'n good :D)

 

<script language="javascript">
function limiter()
{
var max = 250; // maximum number of characters to display
var chatwin = document.getElementById('chat').innerHTML.length;
var deleteThis = chatwin * 0.50; // removes 50% of the chat's characters
if (chatwin >= max) {
document.getElementById('chat').innerHTML = document.getElementById('chat').innerHTML.slice(deleteThis);
}
setTimeout("limiter()", 1);
}
window.onload=function() {
limiter();
}
</script>

<div id="chat">
words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words words
</div>

Link to comment
Share on other sites

Problem

 

What if its like this

<div id="chat">
<div class="message">words words words words words words</div>
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div>
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div>
<div class="message">words words words words words words</div>
<div class="message">words words words words words words</div> 
</div>

 

Which will result in something like this

<div id="chat">
lass="message">words words words words words words</div>
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
<div class="message">words words words words words words</div> 
</div>

 

Hence, chopping bits of code off rendering anything there after problematic

 

Think again :P He he

 

 

 

Link to comment
Share on other sites

You better using DOM when working with AJAX chat or similar scripts, basiclly, you'll need to remove the old divs after a while by set an interval that checks the lenght of comments.

 

For example

 

function removeOldDivs(){
    var maxMessages = 20;
    var msg_divs = document.getElementById('chat').getElementsByTagName('div');
    var numMessages = msg_divs.length;

    if (numMessages > maxMessages){
        var loop = numMessages -  maxMessages;
        for (var i=0; i<loop; i++){
            document.getElementById('chat').removeChild(msg_divs[i]);
        }
    }
};

setInterval("removeOldDivs()", 5*60*1000);  //every 5 minutes

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.