Skip to content

Instantly share code, notes, and snippets.

@DanielOberlechner
Created May 19, 2022 21:30
Show Gist options
  • Save DanielOberlechner/725b2b6482c5abc8fd679d874e15cefa to your computer and use it in GitHub Desktop.
Save DanielOberlechner/725b2b6482c5abc8fd679d874e15cefa to your computer and use it in GitHub Desktop.
Small Javascript Number Counter for Website's
const speed = 10; //Number from 1-? to set the counting speed
const counter = document.querySelector('.js_numberGen'); //query the div or span which shall count
counter.innerHTML += "1";
const updateCount = () => {
const target = parseInt(counter.getAttribute('data-target')); //Set the value to which it should count
const count = parseInt(counter.innerText);
const increment = Math.floor(Math.random() * 10);
if (count < target) {
counter.innerText = count + increment;
setTimeout(updateCount, speed);
} else {
counter.innerText = target;
}
};
updateCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment