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>;
}
): DebouncedParameters
fn— Function to debounce.minWait— Time in milliseconds to wait before executingfnsince the debounced function's last call.optionseager— When set totrue,fnis executed as soon as the debounced function is called iffnis not scheduled andminWaitmilliseconds have elapsed sincefn's last execution (orfnhas not been executed). Defaults tofalse.
Return value
Debounced function with cancel method to cancel scheduled fn execution.