examCountDown.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="exam-count-down-content">
  3. <view v-if="data.status === 'before'">
  4. <view class="exam-count-down-timer">{{formatTime}} </view>
  5. </view>
  6. </view>
  7. </template>
  8. <script setup>
  9. import {
  10. reactive,
  11. onUnmounted,
  12. computed,
  13. watch
  14. } from 'vue';
  15. const STATUS = {
  16. init: null,
  17. moreThenAnHour: 'more',
  18. before: 'before', // 等待参加
  19. after: 'after',
  20. };
  21. const props = defineProps({
  22. count: {
  23. type: Number,
  24. default: 10
  25. }
  26. })
  27. const Emits = defineEmits(['time-end'])
  28. const data = reactive({
  29. curCount: 0,
  30. status: STATUS.init,
  31. timer: null
  32. })
  33. const formatTime = computed(() => {
  34. if (data.curCount > 3600) {
  35. return '';
  36. }
  37. const minute = (Math.floor(data.curCount / 60)).toString().padStart(2, '0');
  38. const second = (Math.floor(data.curCount % 60)).toString().padStart(2, '0');
  39. return `${minute}:${second}`;
  40. })
  41. watch(() => data.curCount, (newVal) => {
  42. if (newVal > 3600) {
  43. data.status = STATUS.moreThenAnHour;
  44. } else if (newVal <= 3600 && newVal > 0) {
  45. data.status = STATUS.before;
  46. } else {
  47. data.status = STATUS.after;
  48. }
  49. }, {
  50. immediate: true
  51. })
  52. // 执行组件 组件启动
  53. function init() {
  54. console.log('init')
  55. data.curCount = props.count;
  56. if (data.curCount > 0) {
  57. countDown(data.curCount);
  58. }
  59. }
  60. // 终止组件 恢复初始化
  61. function termination() {
  62. clearTimer();
  63. resetCurCount();
  64. resetCurStatus();
  65. }
  66. function resetCurCount() {
  67. curCount = 0;
  68. }
  69. function resetCurStatus() {
  70. data.status = STATUS.init;
  71. }
  72. function clearTimer() {
  73. clearInterval(data.timer);
  74. data.timer = null;
  75. }
  76. // 倒计时业务执行
  77. function countDown(TIME_COUNT) {
  78. // 参数非数字 异常禁止执行
  79. if (typeof TIME_COUNT !== 'number') {
  80. return false;
  81. }
  82. // 参数小于等于0 不需要倒计时
  83. if (TIME_COUNT <= 0) {
  84. return false;
  85. }
  86. // 执行业务
  87. if (!data.timer) {
  88. data.timer = setInterval(() => {
  89. // 倒计时存在 执行倒计时业务
  90. if (data.curCount > 0) {
  91. data.curCount--;
  92. if (data.curCount <= 0) {
  93. Emits('time-end', true)
  94. data.clearTimer();
  95. }
  96. }
  97. }, 1000);
  98. }
  99. }
  100. onUnmounted(() => {
  101. clearTimer()
  102. })
  103. defineExpose({
  104. init,termination
  105. })
  106. </script>
  107. <style>
  108. </style>