I have an ajax chat on my website. However, it sometimes happens that messages are duplicated for non-relevant reasons. So I would like to check if the messages I receive in JSON already exist in the chat, if they exist, do not show them and if they do not exist, show them. The code I have is:
var VerificaChat = function(TempoRequest){
$.ajax({
url: 'chat_receive.php',
dataType: 'JSON',
type: 'POST',
data: 'type=recebeMsg&lobby=' + $('.lobbyid').val(),
success: function(data){
if(data.status === 'success'){
for( var i = 0; i < data.msgs; i++){
if(data[i]['time'] !== 'ambos'){
// AQUI EU GOSTARIA DE VERIFICAR AS MENSAGENS MOSTRADAS NO CHAT, E VER SE A MENSAGEM RECEBIDA NO JSON É IGUAL A QUE APARECE NA TELA DO USUÁRIO.
//ABAIXO EU MONTO A MENSAGEM E MOSTRO AOS USUÁRIOS.
$('#chat_box').append('<div class="msg user_' + data[i]['time'] + '"><span>'+ data[i]['nick'] + ': </span>' + data[i]['contente'] +'</div)');
}else{
$('#chat_box').append('<div class="msg ambos"><span>'+ data[i]['nick'] + ' </span>' + data[i]['contente'] +'</div)');
}
}
});
}
<div id="chat_box">
<!--EXEMPLO DE MENSAGEM-->
<div class="msg user_azul"><span>VÍTOR:</span> Oi pessoal</div>
</div>
AJAX has the following return in JSON (the return varies according to the number of new messages):
{
"0":{
"id_user":"10",
"contente":"45454545454",
"time":"azul",
"nick":"ikeda."
},
"1":{
"id_user":"10",
"contente":"4",
"time":"azul",
"nick":"ikeda."
},
"2":{
"id_user":"10",
"contente":"5",
"time":"azul",
"nick":"ikeda."
},
"3":{
"id_user":"10",
"contente":"45",
"time":"azul",
"nick":"ikeda."
},
"4":{
"id_user":"10",
"contente":"44",
"time":"azul",
"nick":"ikeda."
},
"status":"success",
"msgs":5
}