SecondTimer.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <template>
  2. <span class="second-timer">
  3. {{ duration }}
  4. </span>
  5. </template>
  6. <script>
  7. export default {
  8. name: "SecondTimer",
  9. data() {
  10. return {
  11. duration: "00:00:00",
  12. recordSecondCount: 0,
  13. setT: null,
  14. };
  15. },
  16. methods: {
  17. start() {
  18. this.setT = setInterval(() => {
  19. this.recordSecondCount++;
  20. this.duration = this.timeToText(this.recordSecondCount);
  21. }, 1000);
  22. },
  23. end() {
  24. this.pause();
  25. this.duration = "00:00:00";
  26. this.recordSecondCount = 0;
  27. },
  28. pause() {
  29. clearTimeout(this.setT);
  30. this.$emit("on-duration", this.recordSecondCount, this.duration);
  31. },
  32. timeToText(timeNumber) {
  33. const HOUR_TIME = 60 * 60;
  34. const MINUTE_TIME = 60;
  35. let [hour, minute, second] = [0, 0, 0];
  36. let residueTime = timeNumber;
  37. if (residueTime >= HOUR_TIME) {
  38. hour = Math.floor(residueTime / HOUR_TIME);
  39. residueTime -= hour * HOUR_TIME;
  40. }
  41. if (residueTime >= MINUTE_TIME) {
  42. minute = Math.floor(residueTime / MINUTE_TIME);
  43. residueTime -= minute * MINUTE_TIME;
  44. }
  45. second = residueTime;
  46. return [hour, minute, second]
  47. .map((item) => ("00" + item).substr(("" + item).length))
  48. .join(":");
  49. },
  50. },
  51. beforeDestroy() {
  52. if (this.setT) clearInterval(this.setT);
  53. },
  54. };
  55. </script>