Repeat interval

Asked

Viewed 61 times

1

Hello, I did a post asking this: Setar Interval every 1 second

And I have the following code:

iniciarVerificacao();
var verificar;
function iniciarVerificacao(){
   if(verificar) clearInterval(verificar); 
   verificar=setInterval(function() {
      if ($('#botao').is(':visible')){
         console.log('botão vísivel');
         clearInterval(verificar);
         $('#botao').trigger('click');
         console.log('botão removido');
         iniciarVerificacao();
      }else{
         console.log('botão não vísivel');
         iniciarVerificacao(); << Aqui seria para reiniciar e verificar novamente se o botão está vísivel (function iniciarVerificacao()
      }
   }, 1000);
}

What I wanted: That, he checked every 1 second if the button is visible on the page (after an hour (or varies, so the interval of 1 second) the page refreshes and the button appears), and when it appears, it clicks on the button, and an hour of time appears to appear again, and starts the check again. And stay in this infinite cycle, but the interval for when you are removing the button, summarizing:

1->Verifica se o botão está ativo
2->**Para o intervalo para remover o botão**
3->Ativa o intervalo novamente para ver se o botão está ativo (dentro de uma hora)

But I’m having some problems, when the button appears the script gets stopped, I don’t know if it gets stopped because the page updates, or if it stands in the Else, because if you leave the script running forever, it stops in the message: console.log('not visible button');

Does anyone know what it could be?

  • You are using two different ids: #botão and #button, is that right? They are two separate buttons?

  • Add a parameter to the function to specify the time. Ex: function iniciarVerificacao(ms) and you can use }, ms); instead of }, 1000);. And when to call the function iniciarVerificacao(1000); to check every second or iniciarVerificacao(3600000); for checking every hour.

  • I edited the post, I put it wrong, both are put. Valdeir, this code checks, but when you refresh the page the script to, or it stops on Else, I don’t know which of the two.

  • I edited the answer.

1 answer

0


You don’t need so many clearInterval(verificar). It is sufficient that the iniciarVerificacao() never be called, or be called late,

var verificar;
(function iniciarVerificacao() {
    verificar = setTimeout(function () {
        var verificar2;

        if ($('#button').is(':visible') {
            console.log('botão visível');
            $('#button').trigger('click');
            console.log('botão removido');
        } else {
            console.log('botão não visível');
        }

        verificar2 = setTimeout(function () {
            iniciarVerificacao();
        }, 500);
    }, 500);
})();

Also see if the button is no longer deleted by default.

If you want to control when the flow will run through the iniciarVerificacao or not, place a flow deviation,

var devoDesviar = false;
var verificar;
(function iniciarVerificacao() {
    verificar = setTimeout(function () {
        var verificar2;

        if (!devoDesviar) { // condição para desvio de fluxo
            if ($('#button').is(':visible') {
                console.log('botão visível');
                $('#button').trigger('click');
                console.log('botão removido');
            } else {
                console.log('botão não visível');
            }
        }

        verificar2 = setTimeout(function () {
            iniciarVerificacao();
        }, 500);
    }, 500);
})();

(function desvio() {
    setTimeout(function () {
        devoDesviar = !devoDesviar;
        desvio();
    }, 30000); // a cada 30 segundos, chaveio a verificação do botão button.
})();

EDIT: In case the page is being cached, try putting the following tags on it,

<meta http-equiv="Cache-control" content="no-cache"></meta>
<meta http-equiv="Expires" content="-1"></meta>
  • How do I keep it working even if the page updates?

  • Why would the page update?

  • The page automatically updates to the button that appears

  • But the code should work regardless of whether the page updates or not

  • Maybe the button is disabled after refreshing the page? Or the page is curly, maybe?

  • The button is deactivated when it is clicked, then a time of 1 hour appears and when it finishes, the page refreshes and the button is activated. A question, to call the function to start the verification process put the: start Diagnostic()? Because otherwise the function will never start?

  • If you wear it the way I did, (function iniciarVerificacao() { ... })();, do not need to call her. But have to make sure that this code is within a $(document).ready() or window.onload

  • This deviation you said, if I put it, after the button is visible, will give a timeout of 30 seconds to wait for the button to be removed and then the function starts?

  • It was just an example, @Slinidy. You can follow the logic for your needs.

  • Is that the focus of the question is this, I put a timeout to not keep trying to remove the button all the time

  • Try running the code with this example, to see if it works the way you want it to. Change the 30000 for another value, to vary the case.

Show 6 more comments

Browser other questions tagged

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