ant-design/components/_util/throttleByAnimationFrame.ts
lijianan 9aec72c395
chore: remove useless tsx support (#39890)
* chore: remove useless tsx support

* add

* add

* style

* fix lint

* fix lint

* fix lint

* update locale entry

* update locale entry

* update locale entry

* delete useless style
2022-12-29 18:33:13 +08:00

30 lines
623 B
TypeScript

import raf from 'rc-util/lib/raf';
type throttledFn = (...args: any[]) => void;
type throttledCancelFn = { cancel: () => void };
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
let requestId: number | null;
const later = (args: T) => () => {
requestId = null;
fn(...args);
};
const throttled: throttledFn & throttledCancelFn = (...args: T) => {
if (requestId == null) {
requestId = raf(later(args));
}
};
throttled.cancel = () => {
raf.cancel(requestId!);
requestId = null;
};
return throttled;
}
export default throttleByAnimationFrame;