<template>
  <div>
    <p>Dapatkan Kod baru dalam masa : {{ minutes }}:{{ seconds }} saat</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      minutes: 5,
      seconds: 0,
      timer: null // Initialize timer variable
    };
  },
  created() {
    this.startTimer(); // Start the timer when the component is created
  },
  methods: {
    startTimer() {
      // Clear existing timer if it exists
      if (this.timer) {
        clearInterval(this.timer);
      }

      // Start the timer
      this.timer = setInterval(this.countdown, 1000);
    },
    restarttimer() {
      this.startTimer();
      console.log('restarted');
    },
    countdown() {
      if (this.seconds > 0) {
        this.seconds--;
      } else if (this.minutes > 0) {
        this.minutes--;
        this.seconds = 59;
      } else {
        clearInterval(this.timer);
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.timer); // Clear the timer when the component is destroyed
  }
};
</script>
