Timers and other features for Heroes of the Storm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

57 lines
1.3 KiB

  1. <template>
  2. <div class="timer">
  3. <span v-if="displayHours">{{ hours }}:</span><span v-if="displayMinutes">{{ minutes }}</span><span>:{{ seconds }}</span>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'Timer',
  9. data () {
  10. return {
  11. currentTime: this.$attrs.initialTime
  12. }
  13. },
  14. methods: {
  15. startTimer: function () {
  16. this.timerIntevalId = setInterval(() => {
  17. this.currentTime--
  18. if (this.currentTime === 0) {
  19. this.stopTimer()
  20. }
  21. }, 1000)
  22. },
  23. stopTimer: function () {
  24. clearInterval(this.timerIntevalId)
  25. }
  26. },
  27. computed: {
  28. seconds: function () {
  29. return (this.currentTime % 60).toLocaleString('en', {minimumIntegerDigits: 2})
  30. },
  31. minutes: function () {
  32. let minutes = Math.floor(this.currentTime / 60) % 60
  33. return this.displayHours > 0
  34. ? minutes.toLocaleString('en', {minimumIntegerDigits: 2})
  35. : minutes
  36. },
  37. hours: function () {
  38. return (Math.floor(this.currentTime / 3600) % 24)
  39. },
  40. displayMinutes: function () {
  41. return this.minutes > 0 || this.hours > 0
  42. },
  43. displayHours: function () {
  44. return this.hours > 0
  45. }
  46. },
  47. mounted () {
  48. this.startTimer()
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. </style>