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.
 
 
 
 
 

83 lines
2.3 KiB

  1. <!--
  2. Copyright © 2021 Adrien Agez <adrien.agez@pm.me>
  3. This file is part of Nexus Timers.
  4. Nexus Timers is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Nexus Timers is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with Nexus Timers. If not, see <https://www.gnu.org/licenses/>.
  14. -->
  15. <template>
  16. <div class="timer">
  17. <span v-if="displayHours">{{ hours }}:</span><span v-if="displayMinutes">{{ minutes }}</span><span>:{{ seconds }}</span>
  18. </div>
  19. </template>
  20. <script>
  21. 'use strict'
  22. export default {
  23. name: 'Timer',
  24. data () {
  25. return {
  26. currentTime: this.$attrs.initialTime,
  27. timerIntervalId: null
  28. }
  29. },
  30. methods: {
  31. startTimer: function () {
  32. this.stopTimer()
  33. this.timerIntervalId = setInterval(() => {
  34. if (this.currentTime === 0) {
  35. this.stopTimer()
  36. } else {
  37. this.currentTime--
  38. }
  39. }, 1000)
  40. },
  41. stopTimer: function () {
  42. clearInterval(this.timerIntervalId)
  43. },
  44. setTime: function (time) {
  45. this.currentTime = time
  46. }
  47. },
  48. computed: {
  49. seconds: function () {
  50. return (this.currentTime % 60).toLocaleString('en', {minimumIntegerDigits: 2})
  51. },
  52. minutes: function () {
  53. let minutes = Math.floor(this.currentTime / 60) % 60
  54. return this.displayHours > 0
  55. ? minutes.toLocaleString('en', {minimumIntegerDigits: 2})
  56. : minutes
  57. },
  58. hours: function () {
  59. return (Math.floor(this.currentTime / 3600) % 24)
  60. },
  61. displayMinutes: function () {
  62. return this.minutes > 0 || this.hours > 0
  63. },
  64. displayHours: function () {
  65. return this.hours > 0
  66. }
  67. },
  68. mounted () {
  69. this.startTimer()
  70. }
  71. }
  72. </script>
  73. <style scoped>
  74. </style>