From 11635a4b8dbf6c66ab34980099818d964b0d0454 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Thu, 9 Nov 2023 12:35:46 +0200 Subject: [PATCH] Fix timer increment after flagging --- src/__tests__/timer.test.ts | 9 +++++++++ src/timer.ts | 16 ++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/__tests__/timer.test.ts b/src/__tests__/timer.test.ts index bc7cfb2..fac4647 100644 --- a/src/__tests__/timer.test.ts +++ b/src/__tests__/timer.test.ts @@ -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(); +}); diff --git a/src/timer.ts b/src/timer.ts index f6fb124..f31e7f4 100644 --- a/src/timer.ts +++ b/src/timer.ts @@ -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; } @@ -74,6 +78,6 @@ export class FischerTimer { } flagged(): boolean { - return this.timeRemaining() < 0; + return this.timeRemaining() <= 0; } }