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>C undefined servicio familiar navideño - domingo 19 de diciembre a las 11:45 am. Ven a la Iglesia Evangélica Betania y conoce el verdadero significado de la Navidad.
Su
<p id="demo"> <script> document.write(typeWriter()); </script> </p> inserta el valor de retorno de llamar a typeWriter en ese elemento #demo , pero la función no devuelve nada.
No hay una buena razón real para usar document.write de todos modos. Todo lo que necesita hacer es llamar a la función, no usar document.write también.
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>Arreglando otras partes del código:
.innerHTML solo debe usarse cuando se inserta deliberadamente marcado HTML; con texto sin formato, .textContent es más rápido, más seguro y más predecible 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>