I am trying to create a typing effect using Vanilla JS, but for some reason the charAt
function isn't working, and when I replace i
with something like 0, it works, but it spits it all out at once even though it's wrapped in a setTimeout()
function
var sentence = document.getElementsByClassName('sentence')[0];
var words = ['websites', 'apps', 'games'];
var speed = 100;
function type(word) {
for(var i = 0; i < word.length; i++) {
setTimeout(function() {
sentence.innerHTML += word.charAt(i);
}, speed);
}
}
type(words[0]);
* {
font-family: Arial;
}
.container {
display: flex;
align-items: center;
justify-content: center;
}
.cursor {
background: #000;
width: 2px;
height: 15px;
animation: blink 1s steps(5, start) infinite;
}
@keyframes blink {
to { visibility: hidden; }
}
<div class="container">
<div class="sentence">We make </div>
<div class="cursor"></div>
</div>