I
I
imDrOne2019-08-21 14:28:13
JavaScript
imDrOne, 2019-08-21 14:28:13

How to stop the timer when it reaches 00:00:00 and start counting 'up'?

Good day to all! I need to implement a timer that counts back the time difference obtained by subtracting the Set Time (in the format DD.MM.YY hh:mm:ss) and the Current Time (in the same format) and when it counts down to 00:00:00 , then you need it to count already 'up'. How to implement 'stop' and countdown 'up'

<div class="green--text" v-if="operation.idle_el === 1" v-html="getDiff()"></div>

data () {
return {
  moment: moment.locale('ru'),
  diff: null,
  startTime: null,
  endTime: moment('20.08.19 17:50:00', 'DD.MM.YY HH:mm:ss').format('LTS')
}},
methods: {
getDiff () {
  this.diff = moment.utc(moment.duration(this.endTime) - moment.duration(this.startTime)).format('LTS')
  console.log(this.diff)
  // return '<div>' + this.diff + '</div>'
  return this.diff
},
updateCurrentTime () {
  this.startTime = moment().format('LTS')
}
},
created () {
this.startTime = moment().format('LTS')
setInterval(() => this.updateCurrentTime(), 1 * 1000)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-08-21
@xa3apg

It's easier to adapt a ready-made fareez.info/blog/countdown-timer-using-vuejs .
To determine whether the timer is positive or negative, simply count

computed: {
  isNegative() {
    return ((this.now - this.date) / 60000) < 0;
  }
}

And for more accurate rounding, taking into account the minus and plus timers, if minus additionally bring hours and seconds in minutes to math.ceil and if positive to math.floor , otherwise it will differ by 1 digit (instead of -30 minutes, show -31 and vice versa)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question