index.vue 972 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <div class="countdown">{{ data.countdown }}</div>
  3. </template>
  4. <script lang="ts" setup>
  5. defineOptions({
  6. name: 'countdown',
  7. })
  8. const data = reactive({
  9. countdown: '',
  10. })
  11. const props = defineProps<{ seconds: number }>()
  12. const emits = defineEmits(['ticker', 'ender'])
  13. onMounted(() => {
  14. methods.start()
  15. })
  16. const methods = {
  17. start(seconds = props.seconds) {
  18. func.timer({
  19. seconds,
  20. ticker(tick) {
  21. data.countdown = methods.duration(tick)
  22. },
  23. ender(time) {
  24. emits('ender', time)
  25. },
  26. })
  27. },
  28. /** 时长处理 */
  29. duration(times) {
  30. if (times) {
  31. let m: number | string = Math.floor(Number(times) / 60)
  32. let s: number | string = Math.floor(Number(times) % 60)
  33. if (s < 10) {
  34. s = `0${s}`
  35. }
  36. if (m < 10) {
  37. m = `0${m}`
  38. }
  39. return `${m}:${s}`
  40. }
  41. },
  42. }
  43. defineExpose(methods)
  44. </script>
  45. <style lang="scss" scoped>
  46. .countdown {
  47. margin-top: 2rpx;
  48. }
  49. </style>