I am making a typewriter effect from an array of strings, some of which have spaces in them. In strings with spaces, the cursor pauses on the last letter of each word and then skips to the first letter of the next word, instead of advancing with the space. The character counter, however, advances as expected on the spaces.
How can I fix this? I assume I need to do something with
but I've tried everything I can think of.
The cursor is just a right border on a blank div:
var words = ["fun", "exciting", "about not giving up", "being helpful", "being open"],
el = document.getElementById('magic'),
word_counter = 0,
character_counter = 0;
function updateText() {
el.innerHTML = el.innerHTML + words[word_counter][character_counter++];
if (character_counter == words[word_counter].length + 1) {
word_counter++;
character_counter = 0;
el.innerHTML = '';
if (word_counter == words.length) {
word_counter = 0;
}
}
}
setInterval(updateText, 100);
#magic {
color: #777;
border-right: 1px solid #777;
padding-right: 7px;
display: inline;
}
<div id="magic"></div>