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

142
Visualizações
update variable based on self-calling setTimeout based function

I'm trying to create a "typewriter" function and use it to define a variable, so that each character is added individually based on a timeout function. The issue is it is only defining the very first character of the text.

<!DOCTYPE html>
<html>
<body>

<h1>Typewriter</h1>

<p id="demo"></p>

<script>
var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;
var x = "" ;
x = typeWriter() ;

function typeWriter() {
  if (i < txt.length) {

    x += txt.charAt(i) ;
    
    i++;
    setTimeout(typeWriter, speed);
    
    return x ;
  }
}

document.getElementById("demo").innerHTML = x ;
</script>

</body>
</html>

** This is based on W3School's typewwriter How-To example. www.w3schools.com **

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You had a few mistakes:

  1. most importantly, and the reason you were seeing no updates: the line document.getElementById("demo").innerHTML = x; was running only once, when the page first loaded. Your typeWriter function was updating x, but the value of x was never being reflected back in the UI

  2. you don't want to set x = typeWriter(), as this overwrites the empty string that you correctly initially set x as, and means your function is trying to add a 1-character string to a function, which will produce a nonsensical result. You don't care about what typeWriter returns (which is why I've also removed the return x from it - that line does no harm but is completely irrelevant anyway), you just want to execute it, so I've replaced that line with simply typeWriter(). The important part is that now x starts off as the empty string, as you want.

See working version here with the above points fixed:

<!DOCTYPE html>
<html>
<body>

<h1>Typewriter</h1>

<p id="demo"></p>

<script>
var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;
var x = "" ;
typeWriter() ;

function typeWriter() {
  if (i < txt.length) {

    x += txt.charAt(i) ;
    
    i++;
    setTimeout(typeWriter, speed);
    
    document.getElementById("demo").innerHTML = x ;
  }
}
</script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Other way to do the same:

<!DOCTYPE html>
<html>
<body>

<h1>Typewriter</h1>

<p id="demo"></p>

<script>
var txt = 'Lorem ipsum dummy text blabla.';
Array.from(txt).map((v,i,a)=>{setTimeout(()=>{document.getElementById("demo").innerHTML+=v;},50*(i+1));})
</script>

</body>
</html>

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