Utilities · @vueuse/shared v15.0.0 · Reviewed
useThrottleFn
Limit how often a function runs while events keep firing, instead of waiting for activity to stop.
Edit the code and the preview updates as you type.
What it does
A scroll handler that recalculates a reading indicator on every event can do too much work; throttling runs it at a controlled cadence.
The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.
The code
const updatePosition = useThrottleFn(() => {
progress.value = readScrollPosition()
}, 100)
useEventListener(window, 'scroll', updatePosition, { passive: true })Where you'd use it
- Scroll progress and position updates
- Resize-driven expensive calculations
- Rate-limiting pointer movement work
In Nuxt (and other gotchas)
The wrapper is safe to create in setup, but bind browser events only on the client. Pair it with useEventListener so the underlying listener is cleaned up with the component.
When not to use it
Use debounce when the work should happen only after activity pauses, such as search. Throttle does not reduce the cost of each call; keep the callback small.
Links
- Official useThrottleFn docs for every option and type
- Source code on GitHub