Skip to content

Instantly share code, notes, and snippets.

Created November 22, 2018 19:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
timer function to change contents of DOM element at given interval (default: 5s)
// 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