SirChick
08-14-2010, 10:59 PM
Hey,
I have an ajax script for a chat room but wanted some one who knows ajax to tell me if there is a way i can increase its efficientcy if at all...as it calls a php script all the time to check for new messages - was wondering if my method of calls can be improved?
Here is my script:
*I completely self taught from researching so im sure theres bad habits in this and im sure theres a better way if you could please help it would be great!*
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRequestObject();
function sndReq() {
http.open('get', 'chatcheck.php');
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response == '1') {
updateShouts();
}
}
}
function sendMessage()
{
$.ajax({
type: "POST",
url: "chatpost.php",
async: false,
data: "message="+$("#myMessage").val(),
});
$("#myMessage").val("");
return false;
}
function updateShouts(){
$('#chatArea').load('chat.php');
}
setInterval( "sndReq()", 1000 );
I have an ajax script for a chat room but wanted some one who knows ajax to tell me if there is a way i can increase its efficientcy if at all...as it calls a php script all the time to check for new messages - was wondering if my method of calls can be improved?
Here is my script:
*I completely self taught from researching so im sure theres bad habits in this and im sure theres a better way if you could please help it would be great!*
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRequestObject();
function sndReq() {
http.open('get', 'chatcheck.php');
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response == '1') {
updateShouts();
}
}
}
function sendMessage()
{
$.ajax({
type: "POST",
url: "chatpost.php",
async: false,
data: "message="+$("#myMessage").val(),
});
$("#myMessage").val("");
return false;
}
function updateShouts(){
$('#chatArea').load('chat.php');
}
setInterval( "sndReq()", 1000 );