const screenDiv = document.getElementById('screen');
function outputKorv() {
const paragraph = document.createElement('p');
screenDiv.appendChild(paragraph);
const korv = new Array('k', 'o', 'r', 'v');
let index = 0;
while (index < korv.length) {
const letter = document.createTextNode(korv[index]);
setTimeout(console.log(letter), 1000);
index++;
}
}
outputKorv();
When I run the script the letters are written to the console immidiately, and not as I expected with a 1 second gap between them. I've also tried to put the increment in the timeout, but I can't get that to work either. I've tried: setTimeout(index++, 1000)
, setTimeout('index++', 1000)
, and setTimeout(() => index++, 1000)
, but neither works. I've also tried a for loop, and do ... while, but I'm getting the same result with those too. The idea is to output the word 'korv' to an HTML-page one letter at a time. Can someone explain what I'm doing wrong?