Skip to content

Instantly share code, notes, and snippets.

@montalvomiguelo
Last active August 27, 2018 16:42
Show Gist options
  • Save montalvomiguelo/a3b7782b5879c6b87f4f1b3ecd2cebc6 to your computer and use it in GitHub Desktop.
Save montalvomiguelo/a3b7782b5879c6b87f4f1b3ecd2cebc6 to your computer and use it in GitHub Desktop.
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