react-chatbot-test / src /utils /simulateTyping.js
ferrywuai's picture
Modularize chat completion and add loading and typing animation
cc1ccff
// Simulate typing effect by revealing text one character at a time
export function simulateTyping(fullText, onUpdate, onComplete, delay = 20) {
let index = 0;
const interval = setInterval(() => {
onUpdate((prev) => prev + fullText[index]);
index++;
if (index >= fullText.length) {
clearInterval(interval);
onComplete();
}
}, delay);
}