i have a simple chat and the way i do thinks now is like this:
function ajax() {
$.ajax({
url: '/chat/index/json',
type: 'POST',
dataType: "json",
success: function(data) {
// output the html to my chat window
}
});
window.setTimeout("ajax()",5000);
}
$(document).ready(function() {
ajax();
$('#chat').submit(function(e) {
e.preventDefault();
sendMessage();
});
});
function sendMessage()
{
// grab the values from text textarea
$.ajax({
url: '/chat/index/jsave',
type: 'POST',
dataType: "html",
data: message,
success: function(d) {
// empty the textarea
ajax();
}
});
}
i basically call the ajax();
function every 5 seconds. Im not sure if this is the best way to do this because i have a request to the server every 5 sec.
is there a better way of doing this?
thanks