useDebounceFn
Debounce execution of a function.
Debounce is an overloaded waiter: if you keep asking him your requests will be ignored until you stop and give him some time to think about your latest inquiry.
Demo
Delay is set to 1000ms and maxWait is set to 5000ms for this demo.
Button clicked: 0
Event handler called: 0
Usage
import { useDebounceFn } from '@vueuse/core'
const debouncedFn = useDebounceFn(() => {
// do something
}, 1000)
document.addEventListener('resize', debouncedFn)
import { useDebounceFn } from '@vueuse/core'
const debouncedFn = useDebounceFn(() => {
// do something
}, 1000)
document.addEventListener('resize', debouncedFn)
You can also pass a 3rd parameter to this, with a maximum wait time, similar to lodash debounce
import { useDebounceFn } from '@vueuse/core'
// If no invokation after 5000ms due to repeated input,
// the function will be called anyway.
const debouncedFn = useDebounceFn(() => {
// do something
}, 1000, { maxWait: 5000 })
document.addEventListener('resize', debouncedFn)
import { useDebounceFn } from '@vueuse/core'
// If no invokation after 5000ms due to repeated input,
// the function will be called anyway.
const debouncedFn = useDebounceFn(() => {
// do something
}, 1000, { maxWait: 5000 })
document.addEventListener('resize', debouncedFn)
Related Functions
useThrottle
useThrottleFn
useDebounce
useDebounceFn
Recommended Reading
Type Declarations
/**
* Debounce execution of a function.
*
* @param fn A function to be executed after delay milliseconds debounced.
* @param ms A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param opts options
*
* @return A new, debounce, function.
*/
export declare function useDebounceFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeRef<number>,
options?: DebounceFilterOptions
): T
/**
* Debounce execution of a function.
*
* @param fn A function to be executed after delay milliseconds debounced.
* @param ms A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param opts options
*
* @return A new, debounce, function.
*/
export declare function useDebounceFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeRef<number>,
options?: DebounceFilterOptions
): T
Source
Contributors
Anthony Fu
Joe Maylor
Jakub Freisler
wheat