var instanse = false; var state; var mes; var file; function Chat () { this.update = updateChat; this.send = sendChat; this.getState = getStateOfChat; } //gets the state of the chat function getStateOfChat(){ if(!instanse){ instanse = true; $.ajax({ type: "POST", url: "inc/process.php", data: { 'function': 'getState', 'file': file }, dataType: "json", success: function(data){ state = data.state; instanse = false; }, }); } } //Updates the chat function updateChat(){ if(!instanse){ instanse = true; $.ajax({ type: "POST", url: "inc/process.php", data: { 'function': 'update', 'state': state, 'file': file }, dataType: "json", success: function(data){ if(data.text){ for (var i = 0; i < data.text.length; i++) { $('#chat-area').append($("<p>"+ data.text[i] +"</p>")); } } document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight; instanse = false; state = data.state; }, }); } else { setTimeout(updateChat, 1500); } } //send the message function sendChat(message, nickname, avatar, link) { updateChat(); $.ajax({ type: "POST", url: "inc/process.php", data: { 'function': 'send', 'message': message, 'nickname': nickname, 'file': file, 'link': link, 'avatar': avatar }, dataType: "json", success: function(data){ updateChat(); }, }); }
Potrzebuje dorobić do tego anti-flood, żeby jedna osoba mogła wysłac wiadomość np. co 3 sekundy, kombinowałem z setTimout, ale nic z tego nie wyszło i nie mam pomysłu jak to ugryźć...
Dzięki z góry!