Clear json array after Ajax from a PHP file

Asked

Viewed 587 times

1

Does anyone know how to clear a json array via ajax from a php file? I have a setInterval that checks every 2 seconds and receives data from a php file( this data is brought as json array, I can see in Chrome’s XHR or firefox) only instead of deleting and putting the arrays it adds again, thus making the requisitions slower each time. (It returns the id of 4 existing users) This function returns online/offline users of a chat I’m doing

PHP:

 case 'verificar':
        $users = isset($_POST['users']) ? $_POST['users'] : '';
        $retorno = array();

        if($users != ''){
            foreach($users as $id_u){
                $sel = BD::conn()->prepare("SELECT horario, limite FROM usuarios WHERE id = ?");
                $sel->execute(array($id_u));
                $fet = $sel->fetchObject();

                $atual = date("Y-m-d H-i-s");
                $mais1 = date("Y-m-d H-i-s", strtotime("+1 min"));

                if($id_u == $_SESSION['id_user']){
                    $up = BD::conn()->prepare("UPDATE usuarios SET limite = ? WHERE id = ?");
                    $up->execute(array($mais1, $id_u));
                }
     //--------------------------------- A PARTIR DESTA LINHA ELE RETORNA----------------------

                if($atual >= $fet->limite){  
                    $retorno['useronoff'][$id_u] = 'off';
                    unset($retorno[count($retorno)-1]);
                }else{
                    $retorno['useronoff'][$id_u] = 'on';
                    unset($retorno[count($retorno)-1]);
                }
            }
        }

$return = json_encode($return); echo $return; break;

Jquery:

    var antes = -1;
    var depois = 0;
    function verificar(){
        beforeSend: antes = depois;
        $('.innerbox_contatos_search').each(function(){
            var link = $(this);
            var id_u = link.attr("id");
            users.push(id_u);
        });
        var u_online = $('span.online').attr('id');
        users.push(u_online);

        $.post('sys/chat.php',{acao: 'verificar', ids: janelas, users: users}, function(x){
            var users_onlines = x.useronoff;
            for(i in users_onlines){
                $('.innerbox_contatos_search span.type.'+i+'').removeClass('on off').addClass(users_onlines[i]);
            }
            depois += 1;
        }, 'jSON');

    }

    setInterval(function(){
        if(antes != depois){
            verificar();
        }
    }, 2000);

inserir a descrição da imagem aqui

1 answer

1


Just you reset the list of IDS in JQUERY, would look like this

var antes = -1;
    var depois = 0;
    function verificar(){
        users = [];
        beforeSend: antes = depois;
        $('.innerbox_contatos_search').each(function(){
            var link = $(this);
            var id_u = link.attr("id");
            users.push(id_u);
        });
        var u_online = $('span.online').attr('id');
        users.push(u_online);

        $.post('sys/chat.php',{acao: 'verificar', ids: janelas, users: users}, function(x){
            var users_onlines = x.useronoff;
            for(i in users_onlines){
                $('.innerbox_contatos_search span.type.'+i+'').removeClass('on off').addClass(users_onlines[i]);
            }
            depois += 1;
        }, 'jSON');

    }

    setInterval(function(){
        if(antes != depois){
            verificar();
        }
    }, 2000);
  • Thank you, you’ve put me out of my misery :)

Browser other questions tagged

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