Skip to content

throttle

Throttle function used to limit the frequency of function calls. It ensures that the function is not called more than once within a specified time interval.

Added in v0.0.1

Usage

typescript
import { throttle } from 'parsnip-kit'

const handler = () => console.log('Function called')

// Basic throttle usage
const throttled = throttle(handler, 300)
throttled() // console.log is called after 300ms
throttled() // Call is ignored due to throttle

// Throttle with leading and trailing options
const throttledWithOptions = throttle(handler, 300, { leading: true, trailing: true })
throttledWithOptions() // console.log is called immediately and again after 300ms if no other calls are made.

API

Type Parameter

ArgTypeDescription
Textends (...args: any[]) => anyType of function to throttle

Arguments

ArgTypeOptionalDefaultDescription
funcTfalseundefinedThe function to throttle
waitnumberfalseundefinedThe minimum allowed interval between two consecutive calls (in milliseconds).
optionsobjecttrueundefinedOptional parameter object
options.leadingbooleantruefalseWhether to execute the function at the beginning of the wait interval
options.trailingbooleantruetrueWhether to execute the function at the end of the wait interval, if not already executed

Returns

Type
(...args: Parameters<T>) => void