Skip to content

Commit

Permalink
Fix timer increment after flagging
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Nov 9, 2023
1 parent 624a1f5 commit 11635a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/timer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ test('Negative time zeroed', () => {
expect(timer.display()).toBe('0:00');
expect(timer.flagged()).toBeTrue();
});

test('No increment after flagging', () => {
const timer = new FischerTimer(10000, 20000, 1000);
timer.begin();
timer.remaining = 0;
expect(timer.end()).toBeTrue();
expect(timer.display()).toBe('0:00');
expect(timer.flagged()).toBeTrue();
});
16 changes: 10 additions & 6 deletions src/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ export class FischerTimer {
}
const delta = performance.now() - this.reference;
this.reference = null;
const flagged = delta > this.remaining;
this.remaining = Math.min(
this.maximum,
this.remaining - delta + this.increment
);
const flagged = delta >= this.remaining;
if (flagged) {
this.remaining = 0;
} else {
this.remaining = Math.min(
this.maximum,
this.remaining - delta + this.increment
);
}
return flagged;
}

Expand All @@ -74,6 +78,6 @@ export class FischerTimer {
}

flagged(): boolean {
return this.timeRemaining() < 0;
return this.timeRemaining() <= 0;
}
}

0 comments on commit 11635a4

Please sign in to comment.