Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var brand = document.getElementById('brand');
var intervals = [];
function scramble(element, text, time) {
var length = element.innerText.length;
var diff = (length - text.length) * -1;
var count = 0;
var lettersPerIteration = 0;
if (diff !== 0) {
lettersPerIteration = diff / time;
}
var eleId = element.id;
clearInterval(intervals[eleId]);
var tempText = '';
intervals[eleId] = setInterval(function() {
tempText = getRandomString(Math.round(length));
length += lettersPerIteration;
element.innerHTML = tempText;
if (count++ > time) {
clearInterval(intervals[eleId]);
element.innerHTML = text;
}
}, 35);
}
function getRandomString(length) {
var priv = {};
priv.consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w'];
priv.vowels = ['a', 'e', 'i', 'o', 'u', ' '];
priv.result = '';
priv.getLetter = function(datasource) {
var key = Math.floor(Math.random() * datasource.length);
return datasource[key];
}
var loopStat = 0;
if (!length) {
length = 6;
}
while (loopStat < length) {
if (loopStat === null || loopStat % 2) {
priv.result += priv.getLetter(priv.vowels);
} else {
priv.result += priv.getLetter(priv.consonants);
}
loopStat++;
}
return priv.result;
}
// scramble(brand, 'Instrumental Study', 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment