0

My value adds 0.0150 in every 90ms and when value becomes 2.15, 0.0150 is being added in every 80ms, But I want to make two of those functions, I mean that after 3.15 I want 0.0150 to be added in every 70 and etc I wrote this function but not working any solutions ?

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

1 Answer 1

1

Do you mean something like this?

startTimer () {
  let interval = 90

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

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

  setTimeout(this.crashFunction, interval);
}

In your code you're creating two timers that are running at the same time. Every time one of those timers fires it'll create another two timers, leading to an exponential growth in the number of timers running.

If I've understood correctly then you just need a single timer and a bit of suitable logic to determine the current interval.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.