jquery - 向 jquery 计数器添加逗号

标签 jquery count counter

我们有一个动画计数器,可以快速计数。我们需要在数字(以千为单位)中添加一个逗号,但不知道如何添加(显然我们是专家)。

我们所拥有的非常适合柜台:

(function($) {
$.fn.countTo = function(options) {
    // merge the default plugin settings with the custom options
    options = $.extend({}, $.fn.countTo.defaults, options || {});

    // how many times to update the value, and how much to increment the value on each update
    var loops = Math.ceil(options.speed / options.refreshInterval),
        increment = (options.to - options.from) / loops;

    return $(this).each(function() {
        var _this = this,
            loopCount = 0,
            value = options.from,
            interval = setInterval(updateTimer, options.refreshInterval);

        function updateTimer() {
            value += increment;
            loopCount++;
            $(_this).html(value.toFixed(options.decimals));

            if (typeof(options.onUpdate) == 'function') {
                options.onUpdate.call(_this, value);
            }

            if (loopCount >= loops) {
                clearInterval(interval);
                value = options.to;

                if (typeof(options.onComplete) == 'function') {
                    options.onComplete.call(_this, value);
                }
            }
        }
    });
};

$.fn.countTo.defaults = {
    from: 0,  // the number the element should start at
    to: 100,  // the number the element should end at
    speed: 1000,  // how long it should take to count between the target numbers
    refreshInterval: 100,  // how often the element should be updated
    decimals: 0,  // the number of decimal places to show
    onUpdate: null,  // callback method for every time the element is updated,
    onComplete: null,  // callback method for when the element finishes updating
};
})(jQuery);

jQuery(function($) {
    $('.shares').countTo({
        from: 2,
        to: 1826,
        speed: 3000,
        refreshInterval: 50,
        onComplete: function(value) {
            console.debug(this);
        }
    });
});

有什么建议吗?

最佳答案

添加此:

 .replace(/\B(?=(\d{3})+(?!\d))/g, ","));

$(_this).html(value.toFixed(options.decimals))

所以你的代码将是这样的:

function updateTimer() {
                value += increment;
                loopCount++;
                $(_this).html(value.toFixed(options.decimals).replace(/\B(?=(\d{3})+(?!\d))/g, ","));

                if (typeof(options.onUpdate) == 'function') {
                    options.onUpdate.call(_this, value);
                }

                if (loopCount >= loops) {
                    clearInterval(interval);
                    value = options.to;

                    if (typeof(options.onComplete) == 'function') {
                        options.onComplete.call(_this, value);
                    }
                }
            }

关于jquery - 向 jquery 计数器添加逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018608/

相关文章:

jquery - if-not 条件失败 (jQuery)

javascript - 选择一个以某些内容结尾的 div

mysql - 在 MySQL 中跨服务器检查某些数据

android - 单击按钮后重置计数器

javascript - 如何从json字符串中获取值

javascript - jQuery - 如果 left <= 0,将 css 设置为 X 数量

sql查询-如何分别计算一行中的值?

mysql - 在 MySQL 中使用 GROUP BY 在 COUNT 中包含零

c - C语言中的计时器\计数器用于费率计算?

xml - for 中的 XQuery 计数器