Timers and other features for Heroes of the Storm
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

63 linhas
1.5 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. 'use strict'
  8. export default {
  9. name: 'Timer',
  10. data () {
  11. return {
  12. currentTime: this.$attrs.initialTime,
  13. timerIntervalId: null
  14. }
  15. },
  16. methods: {
  17. startTimer: function () {
  18. this.stopTimer()
  19. this.timerIntervalId = setInterval(() => {
  20. this.currentTime--
  21. if (this.currentTime === 0) {
  22. this.stopTimer()
  23. }
  24. }, 1000)
  25. },
  26. stopTimer: function () {
  27. clearInterval(this.timerIntervalId)
  28. },
  29. setTime: function (time) {
  30. this.currentTime = time
  31. }
  32. },
  33. computed: {
  34. seconds: function () {
  35. return (this.currentTime % 60).toLocaleString('en', {minimumIntegerDigits: 2})
  36. },
  37. minutes: function () {
  38. let minutes = Math.floor(this.currentTime / 60) % 60
  39. return this.displayHours > 0
  40. ? minutes.toLocaleString('en', {minimumIntegerDigits: 2})
  41. : minutes
  42. },
  43. hours: function () {
  44. return (Math.floor(this.currentTime / 3600) % 24)
  45. },
  46. displayMinutes: function () {
  47. return this.minutes > 0 || this.hours > 0
  48. },
  49. displayHours: function () {
  50. return this.hours > 0
  51. }
  52. },
  53. mounted () {
  54. this.startTimer()
  55. }
  56. }
  57. </script>
  58. <style scoped>
  59. </style>