mirror of
https://github.com/ant-design/ant-design.git
synced 2026-02-09 02:49:18 +08:00
26 lines
513 B
TypeScript
26 lines
513 B
TypeScript
import raf from '@rc-component/util/lib/raf';
|
|
|
|
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
|
|
let requestId: number | null = null;
|
|
|
|
const later = (args: T) => () => {
|
|
requestId = null;
|
|
fn(...args);
|
|
};
|
|
|
|
const throttled = (...args: T) => {
|
|
if (requestId === null) {
|
|
requestId = raf(later(args));
|
|
}
|
|
};
|
|
|
|
throttled.cancel = () => {
|
|
raf.cancel(requestId!);
|
|
requestId = null;
|
|
};
|
|
|
|
return throttled;
|
|
}
|
|
|
|
export default throttleByAnimationFrame;
|