ant-design/components/_util/throttleByAnimationFrame.ts
lijianan be92498f15
refactor: rewrite Affix CC => FC (#42674)
* fix

* test

* Update index.ts

* fix

* fix

* fix

* fix: fix

* fix: fix

* fix: fix

* fix: fix

* fix: fix

* fix: fix

* update docs

* Update components/affix/index.tsx

Co-authored-by: kiner-tang(文辉) <1127031143@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

* fix

* test: add test case

* Update components/affix/index.zh-CN.md

Co-authored-by: afc163 <afc163@gmail.com>
Signed-off-by: lijianan <574980606@qq.com>

* update demo

---------

Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: kiner-tang(文辉) <1127031143@qq.com>
Co-authored-by: afc163 <afc163@gmail.com>
2023-10-07 18:53:14 +08:00

26 lines
494 B
TypeScript

import raf from 'rc-util/lib/raf';
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
let requestId: number | 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;