Skip to content

useDebounceFn

Category
Export Size
388 B
Last Changed
yesterday
Related

Debounce execution of a function.

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.

Demo

Delay is set to 1000ms and maxWait is set to 5000ms for this demo.

Pending: false

Button clicked: 0

Event handler called: 0

Usage

ts
import { 
useDebounceFn
,
useEventListener
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => {
// do something }, 1000)
useEventListener
(
window
, 'resize',
debouncedFn
)

You can also pass a 3rd parameter to this, with a maximum wait time, similar to lodash debounce

ts
import { 
useDebounceFn
,
useEventListener
} 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 })
useEventListener
(
window
, 'resize',
debouncedFn
)

Optionally, you can get the return value of the function using promise operations.

ts
import { 
useDebounceFn
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => 'response', 1000)
debouncedFn
().
then
((
value
) => {
console
.
log
(
value
) // 'response'
}) // or use async/await async function
doRequest
() {
const
value
= await
debouncedFn
()
console
.
log
(
value
) // 'response'
}

Since 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.

ts
import { 
useDebounceFn
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => 'response', 1000, {
rejectOnCancel
: true })
debouncedFn
()
.
then
((
value
) => {
// do something }) .
catch
(() => {
// do something when canceled }) // calling it again will cancel the previous request and gets rejected
setTimeout
(
debouncedFn
, 500)

Cancel

You can cancel any pending execution by calling the cancel method.

ts
import { 
useDebounceFn
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => {
// do something }, 1000)
debouncedFn
()
// Cancel the pending execution before it runs
debouncedFn
.
cancel
()

This is useful when you need to prevent the debounced function from executing, for example, when a component is unmounted or when user input changes context.

Pending State

You can check if there's a pending execution using the isPending ref.

ts
import { 
useDebounceFn
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => {
// do something }, 1000)
debouncedFn
()
console
.
log
(
debouncedFn
.
isPending
.
value
) // true
// After debounce time elapses or cancel is called
console
.
log
(
debouncedFn
.
isPending
.
value
) // false

This is useful for showing loading indicators or disabling UI elements while waiting for the debounced function to execute.

Flush

You can immediately execute the pending invocation using the flush method.

ts
import { 
useDebounceFn
} from '@vueuse/core'
const
debouncedFn
=
useDebounceFn
(() => {
// do something }, 1000)
debouncedFn
()
// Execute the pending invocation immediately instead of waiting
debouncedFn
.
flush
()

This is useful when you need to ensure the debounced function runs right away, for example, before navigating away from a page or submitting a form.

Type Declarations

ts
export type 
UseDebounceFnReturn
<
T
extends
FunctionArgs
> =
CancelablePromisifyFn
<
T
>
/** * Debounce execution of a function. * * @see https://vueuse.org/useDebounceFn * @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 options Options * * @return A new, debounced, function with isPending, cancel, and flush properties. */ export declare function
useDebounceFn
<
T
extends
FunctionArgs
>(
fn
:
T
,
ms
?:
MaybeRefOrGetter
<number>,
options
?:
DebounceFilterOptions
,
):
UseDebounceFnReturn
<
T
>

Source

SourceDemoDocs

Contributors

Anthony Fu
SerKo
Pierre Côté
Bruno Kalthoff
Robin
Thimo Sietsma
IlyaL
Fernando Fernández
Anthony Fu
vaakian X
azaleta
Joe Maylor
Jakub Freisler
wheat

Changelog

Pending for release...
4aa18 - feat: add flush and isPending to filter system (#5259)
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
c1d6e - feat(shared): ensure return types exists (#4659)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)

Released under the MIT License.