diff --git a/deno.json b/deno.json index 3237275..c004b30 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@117/stopwatch", "description": "An easy-to-use stopwatch for Deno.", - "version": "1.0.1", + "version": "1.0.2", "exports": "./mod.ts", "imports": { "@/": "./", diff --git a/src/createStopwatch.ts b/src/createStopwatch.ts index b68150c..c9db77f 100644 --- a/src/createStopwatch.ts +++ b/src/createStopwatch.ts @@ -6,9 +6,9 @@ * @property {function(): number} getElapsedTime - Returns the elapsed time in milliseconds. */ export type Stopwatch = { - start: () => void; - stop: () => void; - reset: () => void; + start: () => Stopwatch; + stop: () => Stopwatch; + reset: () => Stopwatch; getElapsedTime: () => number; }; @@ -27,23 +27,31 @@ export const createStopwatch = (): Stopwatch => { startTime = Date.now() - elapsedTime; running = true; } + + return this; }, stop() { if (running) { elapsedTime = Date.now() - startTime!; running = false; } + + return this; }, reset() { elapsedTime = 0; + if (running) { startTime = Date.now(); } + + return this; }, getElapsedTime() { if (running) { return Date.now() - startTime!; } + return elapsedTime; }, };