JSON verification in JQUERY

Asked

Viewed 30 times

0

I have an ajax chat on my website. However, sometimes messages are duplicated for reasons not relevant. Therefore, 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 JSON return (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
}

  • I don’t think you understand. The message, in long Lling with ajax, is sometimes returned 2 times, so it is shown 2 times. I want to check if this message has already been shown and not show it again.

  • What should be checked is the content and the nick. If the nick and the content already exist equal in the chat it does not show again.

  • Like, I just don’t want it to show up when it’s 2 messages in a row, you know? Like, Kra can type a message, but it doesn’t follow?

  • 1

    Yeah, that would be a criterion. But I think you can do better: put a data-attribute in each div of the messages with a unique identifier, so just check why there is repeated.

  • But how would I check if it already exists?

  • If the identifier is unique, then each message will have different id’s.

  • Okay, I’ll see here

  • This duplicity comes in JSON. json comes with the latest messages

  • 1

    got it.... I’ll post an answer where you eliminate duplicities based on typed text

  • Okay, I’ll wait

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.