setInterval 5 seconds locking browser

Asked

Viewed 756 times

4

I am developing applications using jQuery/Ajax and it has to refresh page 5 in 5 seconds, it works. But if I start using the application the browser starts crashing because of the code, how to fix this problem?

Follows the code:

<script>
$(document).ready(function() {
agenda();
});
function agenda() {
        $.post('cadastrados_compromiso.php', {
                mes2: "<?php echo $mes2; ?>",
                mes: "<?php echo $mes; ?>",
                ano: "<?php echo $ano; ?>",
                dia: "<?php echo $dia; ?>",
                semana: "<?php echo $semana; ?>",
                agenda: "<?php echo $agenda; ?>",
                login: "<?php echo $login; ?>"
            }, function(resposta) {
                    $("#mudaragenda").html(resposta);
                    var myVar = setInterval(function(){agenda()},5000);
            }, 'html');

}
</script>

It crashes all browsers, I think it starts bugging because it’s very little update time and the browser can’t handle (?)...

2 answers

4


Try changing your role setInterval() by function setTimeout recursively.

For example:

$(MeuAjax); // Inicia seu ajax no DOM
function MeuAjax() {
   $.ajax({ 
      complete: function() {
          // Sua lógica
          setTimeout(MeuAjax, 5000);
      }
   });
}

The function setInterval() has an excessive and unnecessary memory loss

  • Valew, it worked!!!

1

Try changing your structure to this one:

setInterval($.ajax /*referencia para a funcao ajax*/,
 2000, {url: 'suaurl', success: onSuccess, error: onError}/*argumentos passados para o $.ajax*/
);

You can also change the setInterval() for http-equiv="refresh", but it applies refresh to the full page.

<head>
   <meta http-equiv="refresh" content="5">
</head> 

Browser other questions tagged

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