Clearinterval does not work

Asked

Viewed 239 times

1

I have a script that does an Ajax Post from time to time.

upgrade();

$.ajax({
        type: "POST",
        url: "processar.php",
        success: function(resposta){

            $("#resposta").html(resposta);
            $("#resposta").find("script").each(function(i) {
                eval($(this).text());
            });

        },complete: function(){
            setTimeout(function(){atualizar();},5000);
        }

    });

parse.php

if( ($result->status=='OK'){

    echo "<script>window.clearInterval(pisca['".$id_div."']);</script>";

}else{

    echo "<script>pisca['".$id_div."'] = setInterval(function(){ 
    $('#".$id_div."').animate({backgroundColor: 'white', color: 'black'}).delay(100);
    $('#".$id_div."').animate({backgroundColor: '".$fundo."', color: '".$letra."'}).delay(100);
    },100);</script>";

}

I pass the id of the div by Post. But clearInterval does not work.

Some help ?

  • Try declaring the variable pisca in the global scope, that is, at the beginning of your script, outside of any function.

  • 1

    How you are using div id as array index pisca, may be overwriting the reference to some call to setInterval previous. Is there really a need for your PHP to return all this HTML? In your case it would be more practical to return a JSON with the parameters and do the rest in the same JS.

  • @Oeslei is already declared Global.

  • @Andréribeiro But anyway the code should not work this way ?

  • I left an answer I think is more accurate than your solution. If you want to help "repair" or understand why your solution doesn’t work tell me what kind of values can $id_div have? numeric or alpha-numeric?

  • In the question you say "I pass the id of the div by Post."...

Show 1 more comment

1 answer

3


I suggest separating the waters.

What belongs next to the client do on the client side, what belongs next to the server do on the server side. This implies that you do not pass Javascript from server to server eval on the client side.

Suggestion:

var pisca = {};

function atualizar(id) {
    $.ajax({
        type: "POST",
        url: "processar.php",
        dataType: 'json',
        data: id, // este campo está em falta no teu código. Não vejo de que maneira passas a ID...
        success: function (res) {
            animar(res.id, res.status, res.data);
        },
        complete: function () {
            setTimeout(atualizar, 5000);
        }
    });
}

function animar(id, status, data) {
    if (status == 'OK') return clearInterval(pisca[id]);
    pisca.id = setInterval(function () {
        $('#' + id).animate({
            backgroundColor: 'white',
            color: 'black'
        }).delay(100);
        $('#' + id).animate({
            backgroundColor: data.fundo,
            color: data.letra
        }).delay(100);
    }, 100);
}

and in PHP do only:

echo json_encode(
    'id'=>$id_div, 
    'status' => ($result->status), 
    'data' => array('fundo' => $fundo, 'letra' =>$letra)
);
  • I used your idea and it worked perfectly friend. Thank you very much !

  • @Pedroaugusto good! I’m glad I helped.

Browser other questions tagged

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