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

110
Visualizações
SetTimeout not doing its job

I've seen a few questions that look similar to this, but nothing so far that seems to work in my situation. I made a little typing function - it's meant to loop through a series of phrases passed into the element's html attribute, and "type" them out, as if somewhere were typing and erasing each letter one by one.

The HTML, when rendered (it's written in PHP), looks something like this:

<h1>Constant text: <span data-text="Example 1, Example 2, Example 3">Changing Text</span></h1>

The "Changing Text" gets "typed out" and then "erased".

The "typing" function recursively adds letters to the element, one by one, from a string parameter, and the "erasing" function remove letters recursively:

function type (el, letters, delay = 0) {
    if ( letters.length < 1 ) {
        return;
    }

    var letter = letters.shift(),
        html   = el.text();

    el.text(html + letter);

    setTimeout(type(el, letters, delay += 200), delay);
}

function erase (el, delay = 0) {
    if ( el.html().length < 1 ) {
        return;
    }

    var text = el.text();

    el.text(text.substring(0, text.length - 1))

    setTimeout(erase(el, delay += 200), delay);
}

Both functions seem to be working, when debugging with console log. But for some reason, the setTimeout isn't setting a timeout... The letters in each phrase appear and disappear simultaneously...

about 4 years ago · Juan Pablo Isaza
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