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>
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>