ant-design-vue/components/_util/raf.ts

34 lines
725 B
TypeScript
Raw Normal View History

import getRequestAnimationFrame, { cancelRequestAnimationFrame } from './getRequestAnimationFrame';
2020-10-13 14:26:56 +00:00
const oriRaf = getRequestAnimationFrame();
2018-12-13 13:26:21 +00:00
export type RafFrame = {
id: number;
};
2018-12-13 13:26:21 +00:00
// Support call raf with delay specified frame
export default function raf(callback: () => void, delayFrames = 1): { id: number } {
2020-10-13 14:26:56 +00:00
let restFrames: number = delayFrames;
2018-12-13 13:26:21 +00:00
2019-01-12 03:33:27 +00:00
function internalCallback() {
restFrames -= 1;
2018-12-13 13:26:21 +00:00
if (restFrames <= 0) {
2019-01-12 03:33:27 +00:00
callback();
2018-12-13 13:26:21 +00:00
} else {
frame.id = oriRaf(internalCallback);
2018-12-13 13:26:21 +00:00
}
}
const frame = {
id: oriRaf(internalCallback),
};
2018-12-13 13:26:21 +00:00
return frame;
2018-12-13 13:26:21 +00:00
}
raf.cancel = function cancel(frame?: { id: number }) {
if (!frame) return;
2020-10-13 14:26:56 +00:00
cancelRequestAnimationFrame(frame.id);
2019-01-12 03:33:27 +00:00
};