Notification button update

0

I have a notification button in my software where I wanted it to automatically update itself. Then I made the following code:

setTimeout(mostrarNotificacao, 90000);
		
function mostrarNotificacao(){
  //function mostrarModal(idPedidos){
    $(document).ready(function() {

        var url = "../../componentes/php/ff_notificacao_php.php";
        $('.modal-content').load(url,
        function(resultado){
         $('#conteudoNotificacao').html(resultado);
        });
    });
  }

But as long as it's not 1.5 minutes the button does not appear, after this appears, is there any better way to do that?

    
asked by anonymous 18.05.2017 / 04:30

2 answers

2

I believe you want the ShowNotification () function to be called every 1.5 minutes. If so, there are two ways: 1) use the setInterval () function instead of setTimeout () in the example that @ lucas-towers gave; 2) call the setTimeout () again within the showNotification () function, right after executing the main code.

But in both cases, I think it's more appropriate to set the showNotification () function inside $ (document) .ready ().

That is, by mode 1):

 $(document).ready(function() {
     mostrarNotificacao();
     setInterval(mostrarNotificacao, 90000);

     function mostrarNotificacao() {
        var url = "../../componentes/php/ff_notificacao_php.php";
        $('.modal-content').load(url, function(resultado) {
            $('#conteudoNotificacao').html(resultado);
        });
    }
  });

and by mode 2):

 $(document).ready(function() {
     var timerId = 0;
     mostrarNotificacao();

     function mostrarNotificacao() {
        clearTimeout(timerId);
        var url = "../../componentes/php/ff_notificacao_php.php";
        $('.modal-content').load(url, function(resultado) {
            $('#conteudoNotificacao').html(resultado);
            timerId = setTimeout(mostrarNotificacao, 90000);
        });
    }
  });

The advantage of this second mode is that if the load () response takes more than 1.5 minutes, one timeout will not overlap the other.

    
18.05.2017 / 05:39
0

Just add a call before, and then count the time:

mostrarNotificacao()
setInterval(mostrarNotificacao, 90000);

function mostrarNotificacao(){
  //function mostrarModal(idPedidos){
    $(document).ready(function() {

        var url = "../../componentes/php/ff_notificacao_php.php";
        $('.modal-content').load(url,
        function(resultado){
         $('#conteudoNotificacao').html(resultado);
        });
    });
  }
    
18.05.2017 / 04:54