Skip to content

Instantly share code, notes, and snippets.

@fetimo
Created October 19, 2016 07:54
Show Gist options
  • Save fetimo/26aa7a77c735e0f55f2535a40ed6a9a3 to your computer and use it in GitHub Desktop.
Save fetimo/26aa7a77c735e0f55f2535a40ed6a9a3 to your computer and use it in GitHub Desktop.
(function (id) {
'use strict';
var container = document.getElementById(id);
if (!container) {
console.warn('No keep-it-on element found on page.');
}
var text = container.textContent.trim();
container.textContent = '';
var index = 0;
createCharacterPromise(index).then(iterateText);
function iterateText() {
index++;
if (index < text.length) {
createCharacterPromise(index).then(x);
} else {
console.log('written');
}
}
function createCharacterPromise(index) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
appendCharacterToText(text[index]);
resolve(index);
}, generateRandomInteger(30, 100));
});
}
function generateRandomInteger(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function appendCharacterToText(character) {
container.textContent += character;
return character.charAt();
}
})('keep-it-on');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment