I'm trying to create a js object with a method which will print letter by letter with a 5 sec delay between each letter. But right now is not running. It writes it with no delay. Where is my error?
class autoWrite{
constructor(id,text){
this.id = id;
this.text = text;
}
startTyping(index=0){
if(index==this.text.length){
console.log("finish");
}
else{
$("#"+this.id).append(this.text.charAt(index));
setTimeout(this.startTyping(index+1),5000);
}
}
}
a = new autoWrite("body-window","hello world");
a.startTyping();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body-window">
</div>