Forum Discussion
MikeLasky
2 years agoCommunity Member
CountUp with a start, end and duration
Hi All!!!
I wanted a counter to count up to a specific number and stop. Kind of like you see on websites where the number of visitors or certificates earned.
Nothing fancy with regards to the nu...
SarahHodge
2 years agoFormer Staff
Hi Mike! 👋 Welcome to E-Learning Heroes! Thanks for sharing this countup solution with the community. And you made the instructions so easy to follow. Nice!
- MikeLasky2 years agoCommunity Member
Always happy to share fun toys!!!!
Here's the code for a countdown from a number to a number. Not a clock countdown.
//countDown defining start and end numbers along with increment delay.
var player = GetPlayer();
function startCount(start, end, delay) {
var countingDiv = player.GetVar("countUp");
var count = start;
function updateCount() {
countingDiv = count;
count--;
if (count >= end) {
setTimeout(updateCount, delay);
}
//adding the count - 1 as the interval is counted at a fraction above. This will round up to the actual number.
player.SetVar("countUpDisplay", count + 1);
}
updateCount();
console.log("Start at " + start);
console.log("End at " + end);
console.log("Delay in ms: " + delay + " milliseconds between each number");
console.log("Delay in sec: " + delay / 1000 + " seconds between each number");
}
// (Start the count, end the count, delay in ms). For example, 1-20 with a delay with a delay of 1 second between each count would look like this: startCount(1, 20, 1000)
startCount(300, 0, 20);
- SarahHodge2 years agoFormer Staff
Awesome! Thanks for sharing that way too, Mike!