Forum Discussion
Creating an animated count up number, javascript???
I created something a bit more simplistic but less stable with the setInterval method in Javascript.
The following script will add 2500 to a variable every 50 milliseconds until it reaches the target value of 25,000. So in this case it will count to 25,000 in around 0.5 seconds I used this in a cue point trigger:
//Build a bridge to player in Javascript
var player = GetPlayer();
//Get the value variable of "yourVar" from Story file
var toCount = player.GetVar("yourVar")
// Update the count
var x = setInterval(function() {
//The number to count up to (in this case 25000)
if (toCount < 25000) {
//How much to increment per interval iteration
toCount= toCount + 2500;
/*The below segment will format the number and set it to a localized string, in this case British English. I used this particular variant for converting to monetary amount with a comma
This may not be necessary for your purposes depending on what you want to use it for.*/
var countString = toCount.toLocaleString('en-GB')
/* This sets 2 variables, one as an integer and one as a string. If the variable is required again throughout the course, it is essential to have the integer set to the correct number and not the formatted string, as Javascript will not perform correct mathmatical operations on a string.*/
player.SetVar("yourVar",toCount);
player.SetVar("yourVariable",countString);
}
//this line isn't necessary but I always use it for basic debugging
console.log(toCount)
},
//Interval iteration duration in milliseconds e.g. 1000 = 1 second
50);
- CraigOndrak4 years agoCommunity Member
great stuff @damien.
Simple and effective implementation. Exactly what I needed. I just added a couple of lines to make the timer stop after the ticker has reached desirable number. Below is what I added.
else
{
//stop ticking
clearInterval(x);
}console.log(toCount)
Related Content
- 2 months ago