@@ -18,6 +18,8 @@ type Settings = {
1818 timeout : NodeJS . Timeout | null ;
1919} ;
2020
21+ let startTimestamp = 0 ;
22+
2123export let settings : Settings | null = null ;
2224
2325export const caret = new Caret (
@@ -129,7 +131,7 @@ export async function init(): Promise<void> {
129131 } ;
130132}
131133
132- export async function update ( duration : number ) : Promise < void > {
134+ export async function update ( expectedStepEnd : number ) : Promise < void > {
133135 if ( settings === null || ! TestState . isActive || TestState . resultVisible ) {
134136 return ;
135137 }
@@ -141,6 +143,10 @@ export async function update(duration: number): Promise<void> {
141143 incrementLetterIndex ( ) ;
142144
143145 try {
146+ const now = performance . now ( ) ;
147+ const absoluteStepEnd = startTimestamp + expectedStepEnd ;
148+ const duration = absoluteStepEnd - now ;
149+
144150 caret . goTo ( {
145151 wordIndex : settings . currentWordIndex ,
146152 letterIndex : settings . currentLetterIndex ,
@@ -152,11 +158,13 @@ export async function update(duration: number): Promise<void> {
152158 easing : "linear" ,
153159 } ,
154160 } ) ;
161+
162+ // Normal case - schedule next step
155163 settings . timeout = setTimeout ( ( ) => {
156- update ( ( settings ?. spc ?? 0 ) * 1000 ) . catch ( ( ) => {
164+ update ( expectedStepEnd + ( settings ?. spc ?? 0 ) * 1000 ) . catch ( ( ) => {
157165 settings = null ;
158166 } ) ;
159- } , duration ) ;
167+ } , Math . max ( 0 , duration ) ) ;
160168 } catch ( e ) {
161169 console . error ( e ) ;
162170 caret . hide ( ) ;
@@ -169,6 +177,7 @@ export function reset(): void {
169177 clearTimeout ( settings . timeout ) ;
170178 }
171179 settings = null ;
180+ startTimestamp = 0 ;
172181}
173182
174183function incrementLetterIndex ( ) : void {
@@ -243,6 +252,8 @@ export function handleSpace(correct: boolean, currentWord: string): void {
243252}
244253
245254export function start ( ) : void {
255+ const now = performance . now ( ) ;
256+ startTimestamp = now ;
246257 void update ( ( settings ?. spc ?? 0 ) * 1000 ) ;
247258}
248259
0 commit comments