This is where you can share your code for solving the Typewriter Effect exercise ![]()
This is always a fun one for teaching beginners. I usually start with setTimeout and then show how requestAnimationFrame can make it smoother.
function typewriterEffect(element, text, i = 0) {
if (i < text.length) {
element.textContent += text.charAt(i);
setTimeout(() => typewriterEffect(element, text, i + 1), 100);
}
}
// Example usage:
// const myElement = document.getElementById('my-typewriter-div');
// typewriterEffect(myElement, 'Hello, world!');
Clean solution — the recursive setTimeout version is the easiest one to actually follow, and the example usage makes it obvious how to wire it up.
Only thing I’d add is a way to stop or restart it. If you call it twice on the same element the two runs step on each other’s textContent. A little cancel flag would sort that out. Also curious to see the requestAnimationFrame version you mentioned, if you ever post it.