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.– Oeslei
How you are using div id as array index
pisca
, may be overwriting the reference to some call tosetInterval
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.– André Ribeiro
@Oeslei is already declared Global.
– Pedro Augusto
@Andréribeiro But anyway the code should not work this way ?
– Pedro Augusto
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?– Sergio
In the question you say "I pass the id of the div by Post."...
– Sergio