SecondTimer.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. recoding: false,
  15. };
  16. },
  17. methods: {
  18. start() {
  19. this.recoding = true;
  20. this.setT = setInterval(() => {
  21. this.recordSecondCount++;
  22. this.duration = this.timeToText(this.recordSecondCount);
  23. }, 1000);
  24. },
  25. end() {
  26. this.pause();
  27. this.duration = "00:00:00";
  28. this.recordSecondCount = 0;
  29. },
  30. pause() {
  31. this.recoding = false;
  32. clearInterval(this.setT);
  33. this.$emit("on-duration", this.recordSecondCount, this.duration);
  34. },
  35. timeToText(timeNumber) {
  36. const HOUR_TIME = 60 * 60;
  37. const MINUTE_TIME = 60;
  38. let [hour, minute, second] = [0, 0, 0];
  39. let residueTime = timeNumber;
  40. if (residueTime >= HOUR_TIME) {
  41. hour = Math.floor(residueTime / HOUR_TIME);
  42. residueTime -= hour * HOUR_TIME;
  43. }
  44. if (residueTime >= MINUTE_TIME) {
  45. minute = Math.floor(residueTime / MINUTE_TIME);
  46. residueTime -= minute * MINUTE_TIME;
  47. }
  48. second = residueTime;
  49. return [hour, minute, second]
  50. .map((item) => ("00" + item).substr(("" + item).length))
  51. .join(":");
  52. },
  53. },
  54. beforeDestroy() {
  55. if (this.setT) clearInterval(this.setT);
  56. },
  57. };
  58. </script>