mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 17:44:35 +08:00
fix: set requestId to null after fn was cancelled (#34858)
This commit is contained in:
parent
27bb1ea4ae
commit
c4ffbff841
@ -1,20 +1,26 @@
|
|||||||
import raf from 'rc-util/lib/raf';
|
import raf from 'rc-util/lib/raf';
|
||||||
|
|
||||||
export function throttleByAnimationFrame(fn: (...args: any[]) => void) {
|
export function throttleByAnimationFrame<T extends unknown[]>(fn: (...args: T) => void) {
|
||||||
let requestId: number | null;
|
let requestId: number | null;
|
||||||
|
|
||||||
const later = (args: any[]) => () => {
|
const later = (args: T) => () => {
|
||||||
requestId = null;
|
requestId = null;
|
||||||
fn(...args);
|
fn(...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
const throttled = (...args: any[]) => {
|
const throttled: {
|
||||||
|
(...args: T): void;
|
||||||
|
cancel: () => void;
|
||||||
|
} = (...args: T) => {
|
||||||
if (requestId == null) {
|
if (requestId == null) {
|
||||||
requestId = raf(later(args));
|
requestId = raf(later(args));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(throttled as any).cancel = () => raf.cancel(requestId!);
|
throttled.cancel = () => {
|
||||||
|
raf.cancel(requestId!);
|
||||||
|
requestId = null;
|
||||||
|
};
|
||||||
|
|
||||||
return throttled;
|
return throttled;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user