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

91
Visualizações
Why is there an unwanted space in this typewriter function?

This outputs L oading however when i execute this, no compilers errors whatsoever, im so confuzzled, i know how to solve this (just put a space before) but i want to know why its happening

<!DOCTYPE html>
<html>

<body>
  <script>
    var i = 0;
    var txt = 'Loading';

    function type()
    {
      if (i < txt.length) {
        document.body.innerHTML += txt.charAt(i);
        i++;
        setTimeout(type, 50);
      }
    }
    type();
  </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're modifying the body element before it's done rendering. If you wait to start typing until after the page has had a chance to fully load (by calling your first type in onload), it will work as expected.

<!DOCTYPE html>
<html>

<body>
  <script>
    var i = 0;
    var txt = 'Loading';

    function type() {
      if (i < txt.length) {
        document.body.innerHTML += txt.charAt(i);
        i++;
        setTimeout(type, 50);
      }
    }
    window.onload = function() {
      type();
    }
  </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