Spaces:
Running
Running
| import { ref } from 'vue' | |
| export function useTypingAnimation() { | |
| const displayedText = ref('') | |
| let timer: number | null = null | |
| function runTypingAnimation(fullText: string, enabled: boolean): Promise<void> { | |
| return new Promise((resolve) => { | |
| if (!enabled) { | |
| displayedText.value = fullText | |
| resolve() | |
| return | |
| } | |
| displayedText.value = '' | |
| let index = 0 | |
| const typeNextChar = () => { | |
| if (index < fullText.length) { | |
| displayedText.value += fullText[index] | |
| index++ | |
| timer = window.setTimeout(typeNextChar, 30) | |
| } else { | |
| resolve() | |
| } | |
| } | |
| if (timer) { | |
| clearTimeout(timer) | |
| timer = null | |
| } | |
| typeNextChar() | |
| }) | |
| } | |
| return { | |
| displayedText, | |
| runTypingAnimation, | |
| } | |
| } | |