ant-design/components/_util/raf.ts

35 lines
652 B
TypeScript
Raw Normal View History

import raf from 'raf';
interface RafMap {
2018-12-07 16:17:45 +08:00
[id: number]: number;
}
let id: number = 0;
const ids: RafMap = {};
// Support call raf with delay specified frame
export default function wrapperRaf(callback: () => void, delayFrames: number = 1): number {
const myId: number = id++;
let restFrames: number = delayFrames;
function internalCallback() {
restFrames -= 1;
if (restFrames <= 0) {
callback();
delete ids[id];
} else {
ids[id] = raf(internalCallback);
}
}
ids[id] = raf(internalCallback);
return myId;
}
2019-01-03 22:11:42 +08:00
wrapperRaf.cancel = function(pid: number) {
raf.cancel(ids[pid]);
delete ids[pid];
2018-12-07 16:17:45 +08:00
};