Created
October 19, 2016 07:54
-
-
Save fetimo/26aa7a77c735e0f55f2535a40ed6a9a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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