Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useDebounceFn.md
1---2category: Utilities3related: useThrottleFn4---56# useDebounceFn78Debounce execution of a function.910> Debounce is an overloaded waiter: if you keep asking, your requests will be ignored until you stop and give them some time to think about your latest inquiry.1112## Usage1314```ts15import { useDebounceFn, useEventListener } from '@vueuse/core'1617const debouncedFn = useDebounceFn(() => {18// do something19}, 1000)2021useEventListener(window, 'resize', debouncedFn)22```2324You can also pass a 3rd parameter to this, with a maximum wait time, similar to [lodash debounce](https://lodash.com/docs/4.17.15#debounce)2526```ts27import { useDebounceFn, useEventListener } from '@vueuse/core'2829// If no invokation after 5000ms due to repeated input,30// the function will be called anyway.31const debouncedFn = useDebounceFn(() => {32// do something33}, 1000, { maxWait: 5000 })3435useEventListener(window, 'resize', debouncedFn)36```3738Optionally, you can get the return value of the function using promise operations.3940```ts41import { useDebounceFn } from '@vueuse/core'4243const debouncedRequest = useDebounceFn(() => 'response', 1000)4445debouncedRequest().then((value) => {46console.log(value) // 'response'47})4849// or use async/await50async function doRequest() {51const value = await debouncedRequest()52console.log(value) // 'response'53}54```5556Since unhandled rejection error is quite annoying when developer doesn't need the return value, the promise will **NOT** be rejected if the function is canceled **by default**. You need to specify the option `rejectOnCancel: true` to capture the rejection.5758```ts59import { useDebounceFn } from '@vueuse/core'6061const debouncedRequest = useDebounceFn(() => 'response', 1000, { rejectOnCancel: true })6263debouncedRequest()64.then((value) => {65// do something66})67.catch(() => {68// do something when canceled69})7071// calling it again will cancel the previous request and gets rejected72setTimeout(debouncedRequest, 500)73```7475## Recommended Reading7677- [**Debounce vs Throttle**: Definitive Visual Guide](https://kettanaito.com/blog/debounce-vs-throttle)7879## Type Declarations8081```ts82export type UseDebounceFnReturn<T extends FunctionArgs> = PromisifyFn<T>83/**84* Debounce execution of a function.85*86* @see https://vueuse.org/useDebounceFn87* @param fn A function to be executed after delay milliseconds debounced.88* @param ms A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.89* @param options Options90*91* @return A new, debounce, function.92*93* @__NO_SIDE_EFFECTS__94*/95export declare function useDebounceFn<T extends FunctionArgs>(96fn: T,97ms?: MaybeRefOrGetter<number>,98options?: DebounceFilterOptions,99): UseDebounceFnReturn<T>100```101