index.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { defineComponent, reactive, onMounted} from "vue"
  2. import styles from "./index.module.less"
  3. import soundWav from './timer.mp3';
  4. let soundVIdeo: HTMLAudioElement | undefined;
  5. const countdownData = reactive({
  6. isShow: false,
  7. step: 3,
  8. isAnimating: false
  9. })
  10. let _countdownTIme: NodeJS.Timer
  11. export function startCountdown() {
  12. Object.assign(countdownData, {
  13. isShow: true,
  14. step: 3,
  15. isAnimating: false
  16. })
  17. soundVIdeo?.play()
  18. let resolveFun: (value: boolean) => void
  19. _countdownTIme = setInterval(() => {
  20. if (countdownData.step <= 0) {
  21. clearInterval(_countdownTIme)
  22. countdownData.isShow = false
  23. resolveFun(true)
  24. soundVIdeo?.pause()
  25. } else {
  26. countdownData.isAnimating = true
  27. const _time = setTimeout(() => {
  28. clearTimeout(_time)
  29. countdownData.isAnimating = false
  30. countdownData.step--
  31. }, 300)
  32. }
  33. }, 1000)
  34. return new Promise( resolve => {
  35. resolveFun = resolve
  36. })
  37. }
  38. export default defineComponent({
  39. name: "countdown",
  40. setup() {
  41. if(!soundVIdeo) {
  42. soundVIdeo = new Audio(soundWav)
  43. soundVIdeo.load();
  44. }
  45. onMounted(()=>{
  46. soundVIdeo?.pause();
  47. })
  48. return () => (
  49. <>
  50. {countdownData.isShow && <div class={[styles.countdown,countdownData.isAnimating&&styles.isAnimating, styles[`step${countdownData.step}`]]}></div>}
  51. </>
  52. )
  53. }
  54. })