How to check by javascript if you have entered a new li tag through setInterval?

Asked

Viewed 235 times

2

It is possible to ask through javascript if there is something new in a list ul - li?

Like I’m working with a refresh of serInterval, but it is not very dynamic, pq? When the box receives a new message to Scroll Height does not descend automatically..

inserir a descrição da imagem aqui

In this example I just received a new message (Red indicates where the scroll is, Blue new space in Scroll Height). The list ul - li But stay where you were last time.

So is there any way you can signal that you’ve received some new li (message), for the user?

Each li separating messages are named with a class like this:

<li class="colorBox_<?php print $id_m; ?>">...</li> 

// $id_m é o id da mensagem na tabela mysql


CODE setInterval:

var loop = setInterval;

function setLoop(de, para, nome) {
    var url_ss = $("#url_b").val();
    var url_s = $("#url_s").val();

    clearInterval(loop);
        loop = setInterval(function() {
        $.ajax({
            type: "POST",
            url: url_ss+"/956309372.php",
            data: 'de='+de+'&para='+para+'&url_s='+url_s,
            cache: false,
            success: function(html) {
                $("#mensChat div._5chat").html(html);
            }
        });
    }, 1000);
}


CODE that calls the setInterval:

function openWidChat(de,para,nome) {
    var url_s = $("#url_s").val();
    $.ajax({
        url: url_s +"/chat/chat.php",
        data:'de='+de+'&para='+para+'&url_s='+url_s,
        type: "POST",
        cache: false,
        success : function(html){
            $("#mensChat div#chat_"+para).html(html);
            $("._5chat").scrollTop($("._5chat")[0].scrollHeight);
    });

    setLoop(de, para, nome);    
}

Thanks in advance. ;)

  • 1

    Do you have control over the code that inserts the li? The ideal would be to intercept the moment of insertion, instead of checking with setInterval. If you have no control, is that your plugin/API (chat, apparently) does not offer any callback or event when something new is inserted?

  • If this were done in Angular, it would be superfacil :p

  • So, the code that makes the insertion of li, I believe that just insert a class for example, unread, in your setinterval you check if any element has this class, if it has, does the process you need and removes this class.

3 answers

2

There’s a little library called insertionQuery that detects the creation of nodes using a macumba of CSS animation, so it does not generate weight. The interface is not jQuery, although it is similar:

insertionQ("li[class^='colorBox_']").every(function (el) {
    $("._5chat").scrollTop(el.scrollHeight);
});

I didn’t test because I don’t have access to the code to verify that the selectors are correct, etc., but the use of the library is quite simple, if you download the minified version (in the link above) and include it in the page.

0

Maybe it’s easier for you to use one setTimeout inside, to process scroll change after html is changed:

function openWidChat(de,para,nome) {
    var url_s = $("#url_s").val();
    $.ajax({
        url: url_s +"/chat/chat.php",
        data:'de='+de+'&para='+para+'&url_s='+url_s,
        type: "POST",
        cache: false,
        success : function(html){
            $("#mensChat div#chat_"+para).html(html);


            setTimeout(function () {
                $("._5chat").scrollTop($("._5chat")[0].scrollHeight);
            })
    });

    setLoop(de, para, nome);    
}

I’m not going to explain to you why setTimeout "zeroed", because we have this question with answers here on the site:

Why you sometimes need setTimeout with a value of 0 (zero)?

0

Use this event outside your setInterval:

$('ul').bind("DOMSubtreeModified",function(){
   alert("ul foi modificada");
});

If your ajax is just adding new messages, ie li for the message, this will work. But if your ajax updates all the content you will have to adapt it in another way.

Example in jsfiddle.

  • I imagined this, I checked but it really didn’t work because the setInterval it alters all ul, the ideal take the last class of li and then a new case between it gives an alert..

  • 1

    It should work if you put it on a higher element in the hierarchy then. However, this event is part of a discontinued module of the DOM, and it is possible that it will be removed from browsers in the future.

  • I tried to do it this way http://i.imgur.com/4v7gsHl.png, but it always returns saying that this is different, but returns as Undefined ;(

  • I thought of a simpler way, but if this event is to be discontinued it is best not to use it. There are other alternatives using same programming logic. For example leave a global variable that says how many 'li' had in ul. And then by setinterval you check if there is the same amount or if increased.

Browser other questions tagged

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