Notification button update

Asked

Viewed 134 times

0

I have a notification button on my software where I wanted it to automatically update itself. So 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 until the 1.5 minutes the button does not appear, after this appears, there is some better way to do this?

  • Voce wants it to appear at first, and then at intervals?

  • That’s what I’m talking about

2 answers

2


I believe you want the function to show Number() to be called every 1.5 minutes. If so, there are two ways: 1) use the setInterval() function instead of setTimeout() in the example @Lucas-torres gave; 2) recall setTimeout() within the function displaNotificacao(), right after executing the main code.

But in both cases, I think it’s more appropriate to define the function show Rating() within $(Ocument). 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 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 longer than 1.5 minutes, one timeout will not overlap the other.

  • 1

    Eldes, thank you very much, I used method 2. I appreciate your help and for having spent your time.

0

Just add a call first, 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);
        });
    });
  }
  • Lucas, this way the function shows Number() will be called only 2 times: the first time when it is called directly in the first line of the code, and then once more after 1.5 minutes by setTimeout(). If I understand correctly, Gabriel needs to be called on nonstop every 1.5 minutes.

  • I edited the answer

  • @Lucastorres thank you, but as eldes told you, I need every 1.5 minutes to be checked in php. But thank you.

Browser other questions tagged

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