0.4.1 • Published 5 years ago

@lavadrop/throttle v0.4.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

@lavadrop/throttle

npm license Travis Build Status codecov Try @lavadrop/throttle on RunKit

Part of a library of zero-dependency npm modules that do just one thing.

npm

min + gzip | 192 bytes

source

Creates a throttled function that only invokes func at most once per every wait milliseconds.

Usage

import throttle from '@lavadrop/throttle'

const logScrollTop = e => { console.log(`top: ${e.target.scrollTop}`) }
const [throttled, cancel] = throttle(logScrollTop, 100)
window.addEventListener('scroll', throttled)
cancel()

Parameters

NameTypeDescription
funcTFuncThe function to throttle.
waitnumberThe number of milliseconds to which invocations are throttled.
callFirstbooleanSpecifies that func should be invoked on the leading edge of the wait timeout.

Type parameters

NameConstraint
TFunc(...args: any[]) => any

Returns

A new throttled function paired with a cancel function.

Return type

[(...args: Parameters<TFunc>) => any, () => void]