bfintal / Counter-Up

Counter-Up is a lightweight jQuery plugin that counts up to a targeted number when the number becomes visible.
http://bfintal.github.io/Counter-Up/demo/demo.html
GNU General Public License v2.0
746 stars 722 forks source link

Loop the counter on a interval + use "." in separation for more than 1.000.000 #61

Open marcelomgerais opened 6 years ago

marcelomgerais commented 6 years ago

Hi, is possible to have the counter in a loop, in time interval. an e.g: when scroll target to numbers, counter starts, from "0" to " 13.423"; and then, in five seconds (an eg), the counter resumes the count, from 0 to 13.423 again. its possible? like a 'loop' with interval.

2) i want to use 1.300.000, but returns "NAn" for me. how can i use this format of numbers??

Tks a lot!

dnunes commented 6 years ago

Crossposting here from https://github.com/bfintal/Counter-Up/issues/57#issuecomment-390517044 in case anyone finds it through Google and needs the same thing.

First, re-running the counter is kind of a hard question because it requires us to "stop" the looping once the screen scrolls off, so it can restart when it becomes visible again. AFAIK there is no such funcionality on library (Waypoints) Counter Up uses. You could simply run it in a loop forever every 5-secs after it finishes (it would start when the user scrolls and then will keep looping). This is subideal because if the user scrolls again it could be mid-counting and would not start from 0, but is the best you can right now. This change would require a somehow bigger modification to the code (some 6 or 7 lines, maybe). You would need to store the array with the numbers and run the current "shifting" process in a copy of this array, so you can reset the array later to re-run. You would enqueue the restart using the "else" that detects the end of the counting at line 65.

For the second question, currently Counter Up runs a "parseInt" in the data you input and it cannot really understand any complex masks (it accepts commas as thousands separator). But as the code store the pre-calculated data in an array, you can easily pass the number through a masking function before storing it at line 54 with

nums.unshift(MASKING_FUNCTION(newNum, PARAMS_IF_NEEDED));