我的值每90毫秒增加0.0150,当值变为2.15时,每80毫秒增加0.0150,但是我想做两个函数,我的意思是说在3.15之后我希望每70毫秒增加0.0150,以此类推。功能,但没有任何解决方案?

  data:{
    crashValue: 1
  },
  mounted(){
    this.startTimer();
  },
  methods:{
    crashFunction: function() {
      this.crashValue += 0.0150;
      this.startTimer();
    },
    startTimer(){
      setTimeout(this.crashFunction, this.crashValue > 2.15 ? 80 : 90);
      setTimeout(this.crashFunction, this.crashValue > 3.15 ? 70 : 80); // When I add this it value goes up really really fast
    }
  }


          <h2 class="crashNumber">{{ crashValue.toFixed(2) }}x</h2><br />

最佳答案

你的意思是这样吗?

startTimer () {
  let interval = 90

  if (this.crashValue > 2.15) {
    interval = 80
  }

  if (this.crashValue > 3.15) {
    interval = 70
  }

  setTimeout(this.crashFunction, interval);
}


在您的代码中,您正在创建两个同时运行的计时器。这些计时器中的任何一个触发时,都会创建另外两个计时器,从而导致运行的计时器数量呈指数增长。

如果我理解正确,那么您只需要一个计时器和一些适当的逻辑即可确定当前间隔。

关于javascript - 两个setTimeout函数不能单独工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59813112/

10-12 12:29
查看更多