File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ export function getHumanTypingDelay ( index : number , text : string ) {
2+ const char = text [ index ]
3+
4+ // ===== 1. 基础速度(约 90–150 ms)=====
5+ let delay = 90 + Math . random ( ) * 60
6+
7+ // ===== 2. 索引相关:开头慢,中段快,结尾慢 =====
8+ const progress = index / text . length
9+
10+ if ( progress < 0.15 ) {
11+ delay *= 1.3 // 起手慢
12+ }
13+ else if ( progress > 0.85 ) {
14+ delay *= 1.25 // 收尾慢
15+ }
16+
17+ // ===== 3. 字符类型权重 =====
18+ if ( char === ' ' ) {
19+ delay += 40
20+ }
21+
22+ if ( / [ , . ! ? ; : ] / . test ( char ) ) {
23+ delay += 200 + Math . random ( ) * 150
24+ }
25+
26+ // ===== 4. 小概率“走神”停顿 =====
27+ if ( Math . random ( ) < 0.03 ) {
28+ delay += 300 + Math . random ( ) * 500
29+ }
30+
31+ // ===== 5. 下限保护 =====
32+ return Math . max ( 40 , Math . round ( delay ) )
33+ }
34+
35+ export async function sleep ( ms : number ) {
36+ return new Promise ( resolve => setTimeout ( resolve , ms ) )
37+ }
You can’t perform that action at this time.
0 commit comments