I'm trying to create a basic typewriter with pure js.
I'm basically trying to emulate somebody typing with a keyboard in a believable fashion. Issue is, for some reason, my node's text doesn't seem to display and I can't seem to figure out why
function get_random_in_interval(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function typewriter(textinput){
var min = 1;
var max = 3;
var rand = get_random_in_interval(min, max);
var into = document.getElementById('textentry');
for (i = 0; i < textinput.length; i++){
var timer = setInterval(function(){
if (i >= textinput.length){
clearInterval(timer);
}
into.innerHTML += textinput.charAt(i);
}, rand * 1000);
}
}
var test = "Hello, I am a <b>text</b> \n \n I tried doing some freaky stuff";
typewriter(test);
#textentry {
border: 10px solid #2c3e50;
width: 400px;
height: 100px;
background-color: #95a5a6;
font-family: Consolas, monospace;
color: #FFF;
}
<div id="textentry"></div>
My best guess would be that I'm using setInterval()
function incorrectly here. Issue is, I can't really think of where else should I place it. If i placed the setInterval()
outside of for()
loop, then it would work (tested it myself), but It would print the entire string at random intervals and not only the desired char.