Small micro-channel sound program figures for dynamic effects

Problem Description: Recently made a note on the micro-channel small program, the results because the individual subject, did not give approval, but it seems they used it. . . Which relates to a digital cumulative effect achieved, Baidu to an article  http://www.wxapp-union.com/thread-1694-2-1.html , so good, there is a problem is if it is an integer, and digital under not great circumstances, would have been the middle of the process is 0, then the last time directly into the final figures, there is no middle course.

Solution: looked js, increasing the accumulated value of a fixed rounding, instead of continuing in the original have been based on the error (multiple rounded) rounded on again, generally speaking is the addition of a parameter

/**
 * Created by wangyy on 2016/12/26.
 */
'use strict';
class NumberAnimate {

  construction (eight) {
    let def = {
      from: 50, // number at the start 
      Speed: 2000, // total time 
      refreshTime: 100, // refresh time 
      decimals: 2, // number of digits after the decimal point 
      The onUpdate: function () {}, // Update callback function 
      the onComplete: function () {} // callback function is completed 
    }
     the this .tempValue = 0; // accumulated value of the variable display 
    the this .tempRealValue = 0; // accumulating true value of the variable 
    the this .opt = Object.assign (DEF , opt); // ASSIGN configuration parameters passed 
    the this .loopCount = 0; //Counting the number of cycles 
    the this .loops = Math.ceil ( the this .opt.speed / the this .opt.refreshTime); // figures for the number of times 
    the this .increment = ( the this .opt.from / the this .loops); // every accumulation the value of 
    the this .interval = null ; // timer object 
    the this .init ();
  }
  init() {
    this.interval = setInterval(() => {
      this.updateTimer()
    }, this.opt.refreshTime);
  }

  updateTimer() {
    this.loopCount++;
    //增加固定值记录
    this.tempRealValue = this.formatFloat(this.tempRealValue, this.increment);
    this.tempValue = this.tempRealValue.toFixed(this.opt.decimals);
    if (this.loopCount >= this.loops) {
      clearInterval(this.interval);
      this.tempValue = this.opt.from;
      this.opt.onComplete();
    }
    this.opt.onUpdate();
  }
  // solution 0.1 0.3 + 0.2 is not equal to the cumulative fractional accuracy problems 
  formatFloat (num1, num2) {
    let baseNum, baseNum1, baseNum2;
    try {
      baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
      baseNum1 = 0 ;
    }
    try {
      baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
      baseNum2 = 0 ;
    }
    baseNum = Math.pow (10 , Math.max (baseNum1, baseNum2));
    return (num1 num2 * + * baseNum baseNum) / baseNum;
  };
}
export default NumberAnimate;

 

Guess you like

Origin www.cnblogs.com/wangbg/p/12019526.html