Hello guys,
I try to implement a chat application using PHP + MySql. The problem so far is that when I am chatting, I "lose" inputs when a small part of the page goes to be updated. On the other web browsers (Chrome, IE, Opera) I have some delay but at least the words are correct. I think the problem is with the
setTimeout("showMessages()",3000);
but I don't know how to improve the source code or if I have to use something else.
I try to make it as reliable as possible. I don't want to increase the refresh time because when a user "says" something, then the others must be able to see it almost simultaneously.
Any idea please??
Here is the source code:
function keyEvent(e)
{
if (window.event)
{
e = window.event;
}
if (e.keyCode==13)
{
send();
}
}
function getObject()
{
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Ajax is not supported, try a different browser.");
return null;
}
}
function showMessages()
{
httpObject = getObject();
httpObject.open("GET", "apath/show-messages.php?", false);// + Math.random(), false);
httpObject.send(null);
document.getElementById("messages").innerHTML = httpObject.responseText;
setTimeout("showMessages()",3000); //refresh the messages every 3 seconds
}
showMessages();
function send()
{
httpObject = getObject();
httpObject.open("GET", "apath/send.php?message=" + document.getElementById('message').value, false);
httpObject.send(null);
document.getElementById("message").value = "";
}