throttle
functionCreates a throttled function that only invokes func at most once per every wait milliseconds.
Signature
throttle<T extends (...args: any[]) => any>(func: T, wait?: number, options?: { leading?: boolean; trailing?: boolean }): T & { cancel: () => void }Parameters
| Name | Type | Description |
|---|---|---|
func | T | The function to throttle |
wait? | number | The number of milliseconds to throttle invocations toDefault: 0 |
options? | { leading?: boolean; trailing?: boolean } | The options object |
Returns
T & { cancel: () => void } - Returns the new throttled function
Examples
Basic Usage
import { throttle } from 'dashlite'
const throttled = throttle(() => console.log('Hello'), 1000)
throttled()
throttled()
throttled()
// => 'Hello' (immediately, then once more after 1000ms if called again)Scroll Event
import { throttle } from 'dashlite'
const handleScroll = throttle(() => {
console.log('Scrolling...')
}, 200)
window.addEventListener('scroll', handleScroll)Available since version 1.0.0