javascript - Problems Adding Commas To Numbers -
i have implemented comma roundnum
of display total being moved;
$.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); }); }; $('.total').text(roundnum).digits();
however cannot seem same actual counter.
$.fn.countto = function(options) { // merge default plugin settings custom options options = $.extend({}, $.fn.countto.defaults, options || {}); // how many times update value, , how increment 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, // number element should start @ to: 100, // number element should end @ speed: 1000, // how long should take count between target numbers refreshinterval: 100, // how element should updated decimals: 0, // number of decimal places show onupdate: null, // callback method every time element updated, oncomplete: null, // callback method when element finishes updating }; $('.timer').countto({ from: 0, to: roundnum, speed: speed, refreshinterval: 600, oncomplete: function() { console.debug(this); } });
an issue this:
$('.total').text(roundnum)
returns string. so, when try add .digits()
onto end of it:
$('.total').text(roundnum).digits();
it's looking .digits()
method on string, not on jquery object because that's $('.total').text(roundnum)
returns.
there several possible ways go, depending upon how want work. makes sense make digits()
code plain function accepts incoming number or string , returns comma-fied string can this:
$('.total').text(digits(roundnum));