像" loop "一样计算数字



我想像循环一样做这件事,完成后,再次启动类似循环的计数器

样本:

http://jsfiddle.net/4v2wK/226/

// Animate the element's value from x to y:
  $({someValue: 40000}).animate({someValue: 45000}, {
      duration: 3000,
      easing:'swing', // can be anything
      step: function() { // called on every step
          // Update the element's text with rounded-up value:
          $('#el').text(commaSeparateNumber(Math.round(this.someValue)));
      }
  });
 function commaSeparateNumber(val){
    while (/(d+)(d{3})/.test(val.toString())){
      val = val.toString().replace(/(d)(?=(ddd)+(?!d))/g, "$1,");
    }
    return val;
  }
function counter(startVal, endVal) {
    $({someValue: startVal}).animate({someValue: endVal}, {
      duration: 3000,
      complete: function() { counter(startVal, endVal) },
      easing:'swing', // can be anything
      step: function() { // called on every step
          // Update the element's text with rounded-up value:
          $('#el').text(commaSeparateNumber(Math.round(this.someValue)));
      }
     });
}

下面是一个工作示例:JSFiddle

有了这个:

(function counter() {
  $({someValue: 40000}).animate({someValue: 45000}, {
      duration: 3000,
      easing:'swing', // can be anything
      step: function() { // called on every step
          // Update the element's text with rounded-up value:
          $('#el').text(commaSeparateNumber(Math.round(this.someValue)));
      },
      complete: counter
  }); 
})();

http://jsfiddle.net/edgarinvillegas/4v2wK/227/

最新更新