Skip to content

Variable: throttled

const throttled: {<TArgs, ThisArg>(callback, delay, config): (...args) => void; defaults: object; } = throttle

Defined in: packages/core/src/deferred/throttle.ts:107

For legacy compatibility

Type Declaration

Returns a throttled function that ensures the callback is invoked at most once in the specified delay interval. All errors will be gracefully swallowed.

If the throttled function is called multiple times during the delay interval, only the first call will invoke the callback immediately.

If trailing is enabled and if returned function is invoked more than once during the delay interval, the callback runs again at the end of the delay with the most recent arguments.

Type Parameters

TArgs

TArgs extends unknown[]

ThisArg

ThisArg

Parameters

callback

(this, ...args) => unknown

function to be invoked after timeout

delay

number = 50

(optional) interval duration in milliseconds. Default: 50

config

ThrottleOptions<ThisArg> = {}

(optional)

Returns

(...args): void

Parameters

args

...TArgs

Returns

void

Examples

javascript
import { throttle } from '@superutils/core'

const handleChange = throttle(
    event => console.log('Value:', event.target.value),
    300, // throttle duration in milliseconds
)
handleChange({ target: { value: 1 } }) // will be executed
handleChange({ target: { value: 2 } }) // will be ignored
handleChange({ target: { value: 3 } }) // will be ignored

setTimeout(() => {
   handleChange({ target: { value: 4 } }) // will be executed (after 300ms)
   handleChange({ target: { value: 5 } }) // will be ignored
}, 400)
javascript
import { throttle } from '@superutils/core'

const handleChange = throttle(
    event => console.log('Value:', event.target.value),
    300, // throttle duration in milliseconds
)
handleChange({ target: { value: 1 } }) // will be executed
handleChange({ target: { value: 2 } }) // will be ignored
handleChange({ target: { value: 3 } }) //  will be executed

setTimeout(() => {
   handleChange({ target: { value: 4 } }) // will be executed
   handleChange({ target: { value: 5 } }) // will be ignored
}, 400)

defaults

defaults: object

Set the default values This change is applicable application-wide and only applies to any new invocation of throttle().

defaults.onError

onError: undefined = undefined

defaults.trailing

trailing: false = false

Deprecated

use throttle instead