Why is the output of the first code block just 'T' repeated, whereas the second block of code works as expected?
function typeWriter() {
var i = 0,
txt = 'Testing',
speed = 40,
typed = document.getElementById('typewriter');
(function addChar() {
if (i < txt.length) {
typed.innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
})();
}
document.onload = typeWriter();
Second code block:
var i = 0,
txt = 'Testing',
speed = 40;
function typeWriter() {
if (i < txt.length) {
document.getElementById('typewriter').innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
}