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

24 lines
513 B
TypeScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
import getRequestAnimationFrame, {
cancelRequestAnimationFrame as caf,
} from './getRequestAnimationFrame';
const raf = getRequestAnimationFrame();
2018-02-04 11:08:04 +08:00
2019-01-12 11:33:27 +08:00
export const cancelAnimationTimeout = frame => caf(frame.id);
2018-02-04 11:08:04 +08:00
export const requestAnimationTimeout = (callback, delay = 0) => {
2019-01-12 11:33:27 +08:00
const start = Date.now();
function timeout() {
2018-02-04 11:08:04 +08:00
if (Date.now() - start >= delay) {
2019-01-12 11:33:27 +08:00
callback.call();
2018-02-04 11:08:04 +08:00
} else {
2019-01-12 11:33:27 +08:00
frame.id = raf(timeout);
2018-02-04 11:08:04 +08:00
}
}
const frame = {
id: raf(timeout),
2019-01-12 11:33:27 +08:00
};
2018-02-04 11:08:04 +08:00
2019-01-12 11:33:27 +08:00
return frame;
};