Created
May 19, 2022 21:30
-
-
Save DanielOberlechner/725b2b6482c5abc8fd679d874e15cefa to your computer and use it in GitHub Desktop.
Small Javascript Number Counter for Website's
This file contains hidden or 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
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