Question:
I want to display an animated number from 0 to max value y during x seconds. I have tried this following code but it take too much to complete and clear the interval.How do I play this animation during exactly
time
seconds?Answer:
It is better not to depend on the timing ofsetInterval()
, but the real problem in your script is that you use the floored value to decide the new value to print out.It is better to use Window.requestAnimationFrame() and create a
update()
function that prints the current number based on the real time elapsed.If you have better answer, please add a comment about this, thank you!