Utils (@pim.sk/utils)

debounce

Delays function execution until after N ms have passed since the last call. Useful for search inputs, resize/scroll handlers and any rapid-fire events.
import debounce from '@pim.sk/utils/debounce.mjs'

Usage

debounce(fn, wait).start

Delays function execution. Each call resets the timer — fn runs only after the last call + wait ms.

fn = () => { ... }
const db = debounce(fn, 800)    // fn = user function, wait = 800ms
db.start()   // resets timer on each call
db.start()   // only this last call fires fn

.run

Cancels any pending debounce and executes fn immediately (no delay).

db.start()  // pending...
db.run()    // fires NOW, cancels pending
db.start()   // starts 800ms timer
db.run()     // cancels timer, runs fn immediately

.stop

Cancels a pending debounce — fn will NOT be called.

db.start()  // pending...
db.stop()   // cancelled
db.start()   // starts 800ms timer
db.stop()    // cancels — fn never fires

.isEnd()

Returns true when debounce is not active (no pending timer, fn already executed or stopped).

db.isEnd()  →  true | false
db.start()
db.isEnd()   // false — timer still running
// after 800ms...
db.isEnd()   // true  — fn already executed

.onEnd(fn)

Async callback — waits until debounce finishes (fn executed), then runs the callback.

db.start()
db.onEnd( () => { ... } )
const db = debounce(fn, 500)
db.start()
db.onEnd(() => {
    // runs after fn completes
})
v 1.1.2