2016-06-02 4 views
0

roundNum에 대해 쉼표를 구현했습니다.이 중 쉼표는 내가 이동하는 합계를 표시합니다.숫자에 쉼표 추가 문제

$.fn.digits = function(){ 
     return this.each(function(){ 
      $(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")); 
     }); 
    }; 

$('.total').text(roundNum).digits(); 

그러나 나는 실제 카운터와 같은 것을 할 수 없습니다.

$('.total').text(roundNum) 

문자열을 반환 :

$.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 
}; 


$('.timer').countTo({ 
    from: 0, 
    to: roundNum, 
    speed: speed, 
    refreshInterval: 600, 
    onComplete: function() { 
     console.debug(this); 
    } 
}); 
+0

: 아마 무엇에 가장 적합한 단지 수신 번호 나 문자열을 받아들이고 그냥이 작업을 수행 할 수 있도록 쉼표 얘기들이었다 문자열을 반환하는 일반 기능으로 digits() 코드를 확인하는 것입니다 이 스레드 : http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript –

+0

@SteveKline 질문을 읽어 주신다면, 이미 기능을 가지고 있지만 다른 문제를 구현하는 데 문제가 있습니다. –

+0

그 부분을 간과해서 죄송합니다. 사과. –

답변

1

하는 문제가이 때문이다. 그래서 때 다음의 말에 .digits()를 추가하려고 : 그게 무슨 $('.total').text(roundNum) 반환 때문에 그것은 아닌 jQuery를 객체에 문자열에 .digits() 방법을 찾고

$('.total').text(roundNum).digits(); 

.

작동 방법에 따라 이동 방법에는 여러 가지가 있습니다. 이 질문은 이미 대답했다

$('.total').text(digits(roundNum)); 
+0

'$ .fn.countTo'에서''.digits();를 모두 사용할 수 있습니까? –

+0

'obj.method()'를 호출하면, 가지고있는 객체의 타입에 속하는 메소드를 호출해야하며, 작동시킬 올바른 인스턴스 데이터가있는 객체 여야합니다. 그래서,'.digits()'를 호출 할 수 있는지 확신 할 수 있는지 묻는다면 제대로 호출하면됩니다. 그러나'.digits()'가 DOM 요소에서 작동하지 않는다면, jQuery 메서드. jQuery 객체는 DOM 요소 목록을 포함하므로 jQuery 메서드는 DOM 요소 목록에서 직접 작동하는 메서드 여야합니다. '.digits()'를 그렇게 만들 수는 있지만 여기서는 적합하지 않은 것 같습니다. – jfriend00

+0

아, 내가 너를 잡은 것 같아. 그래서 나는 함수에 들어가서'.replace (/ (\ d) (? = (\ d \ d \ d) + (?! \ d))/g, "$ 1" 단지 지금 알아낼 필요가 있습니다 _where_ –