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

186
Visualizações
Javascript included the word undefined as part of the outupt
    var i = 0;
    var txt = 'Christmas Family Service - Sunday 19th December at 11:45 am.  Come along to Bethany Evangelical Church and find out about the real meaning of Christmas.'; /* The text */
    var speed = 50; /* The speed/duration of the effect in milliseconds */
    
    function typeWriter() {
      if (i < txt.length) {
        document.getElementById("demo").innerHTML += txt.charAt(i);
        i++;
        setTimeout(typeWriter, speed);
      }
    }


<div class="announce">

<p id="demo">
<script>

document.write(typeWriter());
</script>

</p>

</div>

Cundefined hristmas Family Service - Sunday 19th December at 11:45 am. Come along to Bethany Evangelical Church and find out about the real meaning of Christmas.

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

0

Your

<p id="demo">
  <script>
    document.write(typeWriter());
  </script>
</p>

inserts the return value of calling typeWriter into that #demo element - but the function does not return anything.

There's no real good reason to use document.write anyway. All you need to do is call the function, not use document.write as well.

var i = 0;
var txt = 'Christmas Family Service - Sunday 19th December at 11:45 am.  Come along to Bethany Evangelical Church and find out about the real meaning of Christmas.'; /* The text */
var speed = 50; /* The speed/duration of the effect in milliseconds */

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<div class="announce">
  <p id="demo"></p>
</div>

Fixing other parts of the code:

  • You only need to select the element once, not every time the loop runs
  • .innerHTML should only be used when deliberately inserting HTML markup - with plain text, .textContent is faster, safer, and more predictable

let i = 0;
const txt = 'Christmas Family Service - Sunday 19th December at 11:45 am.  Come along to Bethany Evangelical Church and find out about the real meaning of Christmas.'; /* The text */
const speed = 50; /* The speed/duration of the effect in milliseconds */

const demo = document.getElementById("demo");
function typeWriter() {
  if (i < txt.length) {
    demo.textContent += txt[i];
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<div class="announce">
  <p id="demo"></p>
</div>

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