Clear json array after Ajax coming from a PHP file

1

Does anyone know how to clean a json array via ajax coming 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 array json, I can see in the chrome or firefox XHR) but instead of deleting and putting the arrays it adds again, thus making the requests slower each time. (It returns the id of 4 existing users) This function returns users online / offline from 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);

    
asked by anonymous 20.03.2015 / 15:10

1 answer

1

Just resetting the IDS list in JQUERY, it 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);
    
20.03.2015 / 15:54