Sin embargo, esto genera L oading cuando ejecuto esto, no hay errores de compilación en absoluto, estoy tan confundido, sé cómo resolver esto (solo pon un espacio antes) pero quiero saber por qué sucede
<!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>Está modificando el elemento del cuerpo antes de que termine de renderizarse. Si espera para comenzar a escribir hasta después de que la página haya tenido la oportunidad de cargarse por completo (llamando a su primer tipo en onload), funcionará como se esperaba.
<!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>