From 0d224823cc4d1e3d9ade273d8feb381ccaaa197b Mon Sep 17 00:00:00 2001 From: arne Date: Mon, 25 Mar 2024 08:02:25 +0100 Subject: [PATCH] Already wrote a `debounce` function (: --- client/src/main.ts | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/client/src/main.ts b/client/src/main.ts index e752664..f9e3ff0 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -47,7 +47,7 @@ const makeRipple = (position: [number, number], radius: number): void => { // event handlers -const throttle = (fn: (...x: X[]) => Y, ms: number) => { +const debounce = (fn: (...x: X[]) => Y, ms: number) => { let lastExecutedAt = -1 return (...x: X[]) => { const now = Date.now() @@ -58,7 +58,7 @@ const throttle = (fn: (...x: X[]) => Y, ms: number) => { } } -const at60fps = (fn: (...x: X[]) => Y) => throttle(fn, 1000 / 60) +const at60fps = (fn: (...x: X[]) => Y) => debounce(fn, 1000 / 60) // create a big ripple when touching the screen canvas.addEventListener('mousedown', at60fps((e) => { @@ -191,17 +191,6 @@ document.addEventListener('DOMContentLoaded', () => { loop() }) -const debounce = (fn: Function, ms: number) => { - let prev = 0 - return (...args: any[]) => { - const now = Date.now() - if (now - prev > ms) { - fn.apply(null, args) - prev = now - } - } -} - window.addEventListener('resize', debounce(() => { ctx.canvas.width = window.innerWidth ctx.canvas.height = window.innerHeight