setInterval of 5 seconds locking the browser

4

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

Follow 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 to bugar because it is very little update time and the browser can not handle (?) ...

    
asked by anonymous 19.08.2014 / 17:15

2 answers

4

Try to change your function 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 setInterval() function has excessive and unnecessary memory expense

    
19.08.2014 / 17:26
1

Try changing your structure to this:

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

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

<head>
   <meta http-equiv="refresh" content="5">
</head> 
    
19.08.2014 / 17:26