useDebounce
Creates a debounced function.
Demo
vue
<script setup>
import { useDebounce } from '@8ctavio/vergil'
const debounced = useDebounce(() => {
toast('🤖')
}, 500)
</script>
<template>
<Btn @click="debounced" label="Debounced Toast"/>
</template>
Definition
ts
interface Debounced {
(this: unknown, ...args: unknown[]): void;
cancel: () => void;
}
function useDebounce(
fn: Function,
minWait: MaybeRefOrGetter<number>,
options?: {
eager?: MaybeRefOrGetter<boolean>;
}
): Debounced
Parameters
fn
— Function to debounce.minWait
— Time in milliseconds to wait before executingfn
since the debounced function's last call.options
eager
— When set totrue
,fn
is executed as soon as the debounced function is called iffn
is not scheduled andminWait
milliseconds have elapsed sincefn
's last execution (orfn
has not been executed). Defaults tofalse
.
Return value
Debounced function with cancel
method to cancel scheduled fn
execution.