Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Delay before recursive function call

I'm trying to set a delay before every recursive function call. Currently, it's returning undefined. The problem might be the scope of the recursive call (inside settimeout and then). I tried it like this:

    function delay(ms) {
        return new Promise(resolve => {
            setTimeout(resolve, ms)
        });
    } 

    const checkIfListElementIsRendered = (className) => {
        delay(10).then(function (res) {
            if (document.getElementsByClassName(className)[0]) {
                return true;
            }
            return checkIfListElementIsRendered(className);
        })
    }

And this:

    const checkIfListElementIsRendered = (className) => {
        if (document.getElementsByClassName(className)[0]) {
            return true;
        }
            
        setTimeout(() => {
            return checkIfListElementIsRendered(className);
        }, 10);   
    }

Any help is appreciated!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to turn checkIfListElementIsRendered to async/await syntax, and await the delay and checkIfListElementIsRendered methods, Also instead return true turn it to promise to be always return a promise.

When you call checkIfListElementIsRendered method outside, you need to resolve it with await or then callback.

function delay(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    });
} 

const checkIfListElementIsRendered = async (className) => {
   await delay(10)
   if (document.getElementsByClassName(className)[0]) {
      return Promise.resolve(true);
   }
   return await checkIfListElementIsRendered(className);
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda