Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
SetInterval brings back previous data

So, I am trying to build a simple function to bring all the messages between two users. Like an chat app. I am using setInterval to refresh the messages in case some of the users inserted an new message in the database, but something it's happening: When I select a conversation, and then select another one, the function is bringing back the messages of the previous conversation for a second before showing the messages of the new selected conversation. Here it is:

    // Load messages
    $(".from_messages").click(function() {
        $("#messageList").empty();
        var from = $(this).attr('user_id');
        var user = <?php echo $_SESSION['user_id']; ?>;
        $("input[name='to']").val(from);
        retrieve(from, user);
    })

    //Interval
    function retrieve(from, user) {
        var interval = setInterval(function() {
            $("#messageList").empty();
            $.ajax({
                url: "server.php",
                method: "post",
                dataType: "json",
                data: {"from": from, "user": user},
                success: function(response) {
                    if(response != "") {
                        $(response).each(function(index, item) {
                            if(item.from_id == user) {
                                $("#messageList").append('<div class="col-12 text-right"><p class="w-50 ml-auto">- '+item.message+'</p></div><br>');
                            } else {
                                $("#messageList").append('<div class="col-12"><p class="w-50">- '+item.message+'</p></div><br>');
                            }
                        })
                    }
                }
            })
        }, 3000)
    }

This is the first selected conversation, with user Sandro

Here is the conversation with Moises. And before, showing it's messages, Sandro ones are being shown for half of a second

If somebody can help me, I would be eternally grateful. And here is a link that I hope could show what I am facing: Video demostration

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

setInterval(f, n) will fire f every n milliseconds until you clear the interval. When you call setInterval, it returns a reference to the interval. Save that reference and call clearInterval(reference) when you want the interval to stop--in this case before creating a new interval when you select a different conversation.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda