@ryusei/light
Version:
<div align="center"> <a href="https://light.ryuseijs.com"> <img alt="RyuseiLight" src="https://light.ryuseijs.com/images/svg/logo.svg" width="70"> </a>
26 lines (22 loc) • 595 B
text/typescript
import { AnyFunction } from '../../../types';
/**
* Returns a function that invokes the provided function at most once in the specified duration.
*
* @since 0.0.1
*
* @param callback - A function to throttle.
* @param interval - A throttle duration in milliseconds.
*
* @return A throttled function.
*/
export function throttle( callback: AnyFunction, interval: number ): () => void {
let timer: ReturnType<typeof setTimeout>;
return function () {
if ( ! timer ) {
timer = setTimeout( () => {
callback();
timer = null;
}, interval );
}
};
}