0

I am building a timer and I have set the following as default values in data:

data: () => {
  return {
    hour: 0;
    min: 0;
    sec: 0;
  }
}

Then I have set the following function to trigger the timer:

methods: {
  startTimer: function () {
      var self = this;
      if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
        preventDefault();
      } else {
        this.interval = setInterval(function () {
          self.totalSeconds += 1;

          self.hour = Math.floor(self.totalSeconds / 3600);
          self.min = Math.floor((self.totalSeconds / 60) % 60);
          self.sec = parseInt(self.totalSeconds % 60);
        }, 1000);
      }
    },
}

Now, whenever I trigger the timer with a button <div @click="startTimer()">add timer</div> by spamming it, the seconds would jump very fast as if two or three timers running concurrently (which I think it is). I want to set preventDefault() but it doesn't seem to work. Any ideas on how to turn off the button if the hour, mins and seconds are all = 0?

2 Answers 2

2

Try to get the click event from the method :

methods: {
  startTimer: function (event) {
      var self = this;
      if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
        event.preventDefault();
      } else {
 ...

and you shouldn't use the () in the template in order to get the event passed as parameter :

<div @click="startTimer">add timer</div

or pass $event as parameter :

<div @click="startTimer($event)">add timer</div
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work with "event.preventDefault();" I have used the () in the template because I have to execute 2 functions within the v-bind click - apparently it wouldn't work if I don't include () within the template. The only solution I have is to set the state as true and toggle the button with v-if by changing it to false after pressing the button, but that doesn't seem like a solution, more like a work around.
Try out @click="startTimer($event)"
Actually it worked now with ($event) after I have reloaded localhost. Thank you!
1

call the function without the parenthesis ()

<div @click="startTimer">add timer</div

And then in the startTime function the event will automatically be injected by vuejs which you can use to preventDefault.

methods: {
  startTimer: function (event) {
      var self = this;
      if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
        event.preventDefault();
      } else {
        this.interval = setInterval(function () {
          self.totalSeconds += 1;

          self.hour = Math.floor(self.totalSeconds / 3600);
          self.min = Math.floor((self.totalSeconds / 60) % 60);
          self.sec = parseInt(self.totalSeconds % 60);
        }, 1000);
      }
    },
}

More information

2 Comments

Thanks for the help, I have followed @Boussadjra's suggestion on adding @click="startTimer($event) and it worked.
Actually, as per the documentation there's absolutely no need to do it manually. Event should always be injected by vuejs. Please have a look at the documentation on best practices, as I'm sure that approach will cause issue for you in future. Nevertheless, you are free to use the approach you are comfortable with. Thanks

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.