timer function to change contents of DOM element at given interval (default: 5s)
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
// first iteration | |
// v0.1 | |
(function(globalObject, document) { | |
function randomItem(collection) { | |
var index = Math.floor(Math.random() * collection.length); | |
return collection[index]; | |
} | |
function roll(element, values, time) { | |
time = time || 5000; | |
setTimeout(function() { | |
element.innerHTML = randomItem(values); | |
roll(element, values, time); | |
}, time); | |
} | |
globalObject.roll = roll; | |
})(window, document); | |
// usage: | |
roll(document.querySelector('#theElement'), ["value1", "value2", "value3"]); | |
roll(document.querySelector('#theOtherElement'), ["value1", "value2", "value3"], 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment