Hello guys, I wanted to add an animated number element to my website.
Where the Number will start count from 0 to 24,000.
I’ m using the following code for my Text Element:
let startNum = 0;
let endNum = 23978;
const duration = 10; // 1000 milliseconds
$w.onReady(function () {
setInterval(()=> {
countUp();
}, duration);
});
function countUp(){
if (startNum <= endNum ){
$w('#text112').text = startNum.toLocaleString().toString();
startNum++;
}
}
The code works great for counting - but … counting from 0 to larger number 24,000 it takes a long time! I tried changing the duration, but apparently I don’t know exactly how to get it to count to in just a few seconds.
Look, in the code you posted you set it to add +1 every 10 milliseconds which means it’ll complete counting from 0 to 24,000 within 240 seconds.
If you want it to be faster you can make the interval shorter (but not less than 4 ms) + instead of adding + 1 every time, you can add +100. For example:
function countUp(){
if (startNum <= endNum ){
$w('#text112').text = startNum.toLocaleString();
startNum += 100;
}
}
Is it possible to add a second text box that also animates? I used the code and switched ‘#text1’ for ‘#text2’, and it will either not animate either or only allow one to animate.
@info21816 Actually, it wasn’t David …it was Shan. Before that there are plenty of other coders to created this animated counter back in 2017 when wix code was just released. So will find a lot of old threads with the correct working code.
Just the same … that code listed above has too many onReady lines. There should only be 1 on the page. The code needs to be combined differently.
I actually want a number counter ,in the previously typed manner, as i am a non coder so i needed it in a html code form which i can embed in the website
No need to pay for an animated number counter. Use the simple method below to have animated counter text for FREE
Add a text box and in DEV MODE, paste the following text. Just make sure, <#text1> matched the ID of the text box you are trying to animate. The following code will run the text animation ONCE , when it enters in the the first time.
$w . onReady ( function () {
$w . onReady (()=>{ let viewcounting = 0 //initiate viewportenter count
$w ( ‘#text1’ ). onViewportEnter (()=>{
viewcounting = viewcounting + 1 //increment viewport count by 1 on entry if ( viewcounting === 1 ) { //if statement to check if viewport entry count = 1 let count = 0 //starting number const endTime = 12 // ending number const interval = 100 // spped of the count in ms const counter = setInterval (() => {
$w ( ‘#text1’ ). text = String ( count ) // update text
count === endTime && clearInterval ( counter )
count ++
}, interval )