Skip to content

Instantly share code, notes, and snippets.

@nhanb
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhanb/9950660cb359d375933f to your computer and use it in GitHub Desktop.
Save nhanb/9950660cb359d375933f to your computer and use it in GitHub Desktop.
Typewriter effect in plain javascript
/**
* content: string to be "typed out"
* el: parent element to write content in
* delay: miliseconds between each char
*
* Example usage:
* typewriter("Look ma, no hands!", document.getElementById("container"), 100);
*/
var typewriter = function(content, el, delay) {
var p = document.createElement('p');
el.appendChild(p);
var j = 0;
var interval = window.setInterval(function() {
p.innerHTML += content.charAt(j);
j++;
if (j >= content.length) {
clearInterval(interval);
}
}, delay);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment