Setar Interval every 1 second

Asked

Viewed 355 times

1

I have a problem: To check if the button is visible (He’s got 30 minutes to show up. But as the function varies to remove it, it can increase the time for it to appear so I think it best to use 1 second Interval), needs to set a 1 second interval:

setInterval(function() {
if ($('#botão').is(':visible')){
FUNÇÃO "X"

}
}, 1000)

Once the button was visible initiate a function "X" that removed the button in a time of 15 to 20 seconds Note: may vary and would only perform this function once.And maybe the "X cannot remove the button, then cancels the interval or sends an Alert saying that the button has not been removed".

However if I select the interval of 1 second, it is running the function "X" all the time, because the button is visible in that time of 15 to 20 seconds that the function "X" tries to remove the button

How can I make it so that I don’t run the "X" function all the time and do for example:

If the function "X" is being executed, the interval of 1 second to. And if the function does not remove the button, it cancels the interval or sends an Alert saying that the button has not been removed or returns the interval. If you can tell me what those three roles look like,.

Does anyone have any idea how I can do that? Thanks

  • It seems to me you should use the setTimeout() instead of setInterval(). Explain your problem better and include the code to your question.

  • But the button appears in a 30 minute time frame, I forgot to mention

1 answer

0


Create your setInterval and save to a variable, then when to execute the function you remove the range by the created variable.

Example:

var verificar;
function iniciarVerificacao(){
    if(verificar) clearInterval(verificar); // Isso evita que o intervalo rode mais de uma vez.
    verificar=setInterval(function() {
        if ($('#botão').is(':visible')){
            clearInterval(verificar);
            FUNÇÃO "X"
        }
    }, 1000);
}
iniciarVerificacao(); // Chame quando desejar reiniciar.
  • Ok, and how do you return the range if the X function does not remove the button? Because the range is canceled in clearInterval(check) right? Or continue in the interval of 1 second?

  • 1

    You can set the range in a function. Run the function as soon as the script starts and if you want to restart call the function again. As I just edited in my reply.

  • But if I want to restart using: start?

  • yes, as long as the function starts Scan() is within the same script or can be accessed globally by other js files. (Functions within jQuery call "$(Function(){ your code });" cannot be called from outside of it)

Browser other questions tagged

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