Counter-Up icon indicating copy to clipboard operation
Counter-Up copied to clipboard

Support time format

Open orenivr opened this issue 10 years ago • 1 comments

Hello Benjamin, First I want to say great job.

Second, in my project, I was asked to introduce hours and time format So I added a few lines to add your project support times.

Feel free to add to your project And please DO NOT give me credit for this !!! Because the change is minor and insignificant .

Tnx Oren

(function( $ ){ "use strict";

$.fn.counterUp = function( options ) {

    // Defaults
    var settings = $.extend({
        'time': 400,
        'delay': 10
        }, options);

    return this.each(function(){

        // Store the object
        var $this = $(this);
        var $settings = settings;

        var counterUpper = function() {
            var divisions = $settings.time / $settings.delay;
            var num = $this.attr('data-value');
            var nums = [num];
            var isComma = /[0-9]+,[0-9]+/.test(num);
            num = num.replace(/,/g, '');
            var isInt = /^[0-9]+$/.test(num);
            var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
            var isTime = /^[0-9]+\:[0-9]+\:[0-9]+$/.test(num);
            if (isTime) {
                num  = new Date('1970-01-01T' + num + 'Z').getTime() / 1000;  
            }

            var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;

            // Generate list of incremental numbers to display
            for (var i = divisions; i >= 1; i--) {

                // Preserve as int if input was int
                var newNum = parseInt(num / divisions * i);

                // Preserve float if input was float
                if (isFloat) {
                    newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
                }

                // Preserve commas if input had commas
                if (isComma) {
                    while (/(\d+)(\d{3})/.test(newNum.toString())) {
                        newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
                    }
                }

                // Preserve time if input was in time format 00:01:59
                if (isTime) {
                    newNum = (new Date(newNum * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
                }

                nums.unshift(newNum);
            }

            $this.data('counterup-nums', nums);
            $this.text('0');

            // Updates the number until we're done
            var f = function() {
                $this.text($this.data('counterup-nums').shift());
                if ($this.data('counterup-nums').length) {
                    setTimeout($this.data('counterup-func'), $settings.delay);
                } else {
                    delete $this.data('counterup-nums');
                    $this.data('counterup-nums', null);
                    $this.data('counterup-func', null);
                }
            };
            $this.data('counterup-func', f);

            // Start the count up
            setTimeout($this.data('counterup-func'), $settings.delay);
        };

        // Perform counts when the element gets into view
        $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true });
    });

};

})( jQuery );

orenivr avatar Oct 28 '15 10:10 orenivr

In my fork I merged a bunch of PRs - it's already on bower and NPM.

There was a PR for time support, can you tell me if it's working for you? Thanks!

ciromattia avatar Mar 24 '16 11:03 ciromattia