[Vuejs]-Set interval and sum random floating numbers

0👍

The type of toFixed is string. You can use Number class to convert to float.

Example:

-: 3 + (Math.random() * 1 + 1).toFixed(2) = 3 + "1.58" = 31.58 != 4.58

-: Number((Math.random() * 1 + 1).toFixed(2))+Number((Math.random() * 1 + 1).toFixed(2)) = 3.27 [some float]

export default {
  name: "TableFields",
  props: {
    msg: String,
  },
  data() {
    return {
      initialValue: 3,
      randomNumber: Number(Math.random() * 1 + 1),
      interval: setInterval(this.addition, 2000),
    };
  },
  computed: {},
  methods: {
    addition() {
      this.initialValue = Number(
        (this.initialValue + this.randomNumber).toFixed(2)
      );
    },
    stopInterval() {
      clearInterval(this.interval);
    },
  },
  mounted() {
    // const test = this.initialValue
    console.log(this.randomNumber);
  },
};

Leave a comment