jaymc Posted February 4, 2008 Share Posted February 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 4, 2008 Share Posted February 4, 2008 You can erase all the content; like so: document.getElementById(‘messages’).innerHTML =''; Quote Link to comment Share on other sites More sharing options...
jaymc Posted February 4, 2008 Author Share Posted February 4, 2008 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? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 4, 2008 Share Posted February 4, 2008 try this (it's not perfect; but it's pretty freak'n good ) <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> Quote Link to comment Share on other sites More sharing options...
jaymc Posted February 4, 2008 Author Share Posted February 4, 2008 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 He he Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 4, 2008 Share Posted February 4, 2008 Hence, chopping bits of code off rendering anything there after problematic Think again He he Well I guess you'll have to figure that one out - he he Quote Link to comment Share on other sites More sharing options...
nogray Posted February 5, 2008 Share Posted February 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
jaymc Posted February 5, 2008 Author Share Posted February 5, 2008 Thats great! Didnt quite work but I amended it and its doing what I want Much appreciated guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.