Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

94
Vistas
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

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.