js加载从0到80变化过程代码,让其4s中加载完毕

function textAnimation(start, end, sec){
      var val = start;
      var delay = sec * 1000 / Math.abs(end - start);
      if(delay < 1000 / 60){
        delay = 1000 / 60;
      }
      var step = (end - start) / (sec * 1000 / delay);
      var interval = setInterval(function () {
        if (val < end) {
          // el.innerText = val;
          self.setState({
            progressPer: val
          })
          val += step;
          val = Math.floor(val);
        }else{
          console.log('ended');
          // el.innerText = end;
          self.setState({
            progressPer: end
          })
          clearInterval(interval);
        }
      }, delay);
    }
    
    textAnimation(0, 100, 4);

  

posted @ 2021-01-06 21:06  wj704  阅读(181)  评论(0编辑  收藏  举报