squiblo Posted June 14, 2010 Share Posted June 14, 2010 I am working on a small chat feature and to be honest its working near enough perfect, there is just one more improvement I think I could make. The problem is that when the page loads the cursor style is "progress" because my chat keeps looping and looping never ending because it is always checking if "file.txt" has been updating by a user in the chat. Here is my code. function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g,""); } var xmlhttp1input; function chat1() { showUser1(); } function showUser1(str) { xmlhttp1input=GetXmlHttpObject1(); if (xmlhttp1input==null) { alert ("Browser does not support HTTP Request"); return; } var url1="http://localhost/includes/chat/chat_output/sql_output/sql1.php"; url1=url1+"?q="+str; url1=url1+"&sid="+Math.random(); xmlhttp1input.onreadystatechange=stateChanged1; xmlhttp1input.open("GET",url1,true); xmlhttp1input.send(null); } function stateChanged1() { if (xmlhttp1input.readyState==4) { var response = trim(xmlhttp1input.responseText); var innerdiv = trim(document.getElementById("chatoneoutput").innerHTML); var count_response = trim(xmlhttp1input.responseText).split(" ").length; var count_innerdiv = trim(document.getElementById("chatoneoutput").innerHTML).split(" ").length; if (count_innerdiv != count_response){ document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText; var filelines_count = trim(xmlhttp1input.responseText).split("\n").length; var line = trim(xmlhttp1input.responseText).split("\n"); var username = document.getElementById('username_js').value; var user_id = document.getElementById('user_id_js').value; var lastline_check = username + user_id; var last_line = line[filelines_count - 1]; var matchPos1 = last_line.search(lastline_check); var sound_enabled = document.getElementById('sound_enabled_js').innerHTML; if (sound_enabled == 1) { if (matchPos1 == -1) { (function alert_sound() { var audio = $('<audio />', { autoplay : 'autoplay' }); addsource(audio, 'http://localhost/sounds/chat_sound.ogg'); addsource(audio, 'http://localhost/sounds/chat_sound.mp3'); audio.appendTo('body'); function addsource(elem, path) { $('<source />').attr('src', path).appendTo(elem); }; })(); } } } showUser1(); } } My main question I would like answered - is there a better way to do the same thing I have done above? If you have any questions about the above code then I will do my best to explain. I am using .txt files to save the chats basically just because I want to, but if you can persuade me to use SQL then I might just think about it. Thanks Quote Link to comment Share on other sites More sharing options...
squiblo Posted June 15, 2010 Author Share Posted June 15, 2010 Could somebody please take a look, I've been struggling for a few weeks now. 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.