【问题标题】:Counting number like " loop "像“循环”一样计数
【发布时间】:2014-01-11 21:50:20
【问题描述】:

我想像循环一样执行此操作,完成后,再次像循环一样启动计数器

示例:

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)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
  }

【问题讨论】:

  • 您希望它从相同的 40,000 到 45,000 开始?
  • 是或其他数字也可能很棒

标签: javascript jquery loops numbers counting


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    有了这个:

    (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/

    【讨论】:

    • 我喜欢同步函数:-P
    猜你喜欢
    • 2020-06-27
    • 2017-03-08
    • 1970-01-01
    • 2019-05-27
    • 2015-03-20
    • 2011-03-10
    • 2020-03-11
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多