Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tincho
Created November 22, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tincho/6927c5736923dd80c3b70d0e412f347c to your computer and use it in GitHub Desktop.
Save tincho/6927c5736923dd80c3b70d0e412f347c to your computer and use it in GitHub Desktop.
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