Setar Interval every 1 second [closed]

1

I have a problem: To check if the button is visible (it takes 30 minutes to appear.) But as the function varies to remove it, it can increase the time for it to appear so I think it is better to use Interval 1 second) , you need to set a 1-second interval:

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

}
}, 1000)

As soon as the button was visible it would start an X function that would remove the button in 15-20 seconds time Note:

But if I close the interval of 1 second, it will execute the function "X" all the time, because the button is visible in that time of 15 to 20 seconds that the function "X" / strong>

How can I make sure that I do not run the "X" function all the time and do for example:

If the "X" function is running, the 1 second interval stops. And if the function does not remove the button, then it cancels the interval or sends an alert saying that the button has not been removed or the interval returns. If you can tell me how these 3 functions are, thank you very much.

Does anyone have any idea how I can do this? Thanks.

    
asked by anonymous 01.06.2018 / 16:42

1 answer

0

Create your setInterval and save it to a variable, then when it is 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.
    
01.06.2018 / 16:59