baozhengrui

导航

模拟打字

// 模拟打字
        typeEffect(arrindex, text) {

            let index = 0;
            const typeNextChar = () => {
                if (index < text.length) {

                    // 每次增加一个字符
                    if (this.chatMessages[arrindex]) {
                        this.chatMessages[arrindex].content += text[index];
                        index++;
                        // 随机生成下一个字符的打字速度
                        const randomSpeed = this.getRandomSpeed(this.minSpeed, this.maxSpeed);
                        // 动态定时调用下一个字符输出
                        setTimeout(typeNextChar, randomSpeed);
                    } else {

                        // 输出完毕,显示发送按钮
                        return this.sendDisabled = false
                    }
                } else {
                    this.sendDisabled = false
                }
            };
            typeNextChar();
        },
        // 生成随机速度
        getRandomSpeed(min, max) {
            // 生成范围[min, max]内的随机数
            return Math.floor(Math.random() * (max - min + 1)) + min;
        },

posted on 2025-04-25 16:17  芮艺  阅读(21)  评论(0)    收藏  举报