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

31 lines
538 B
JavaScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
import raf from 'raf';
2018-12-13 21:26:21 +08:00
2019-01-12 11:33:27 +08:00
let id = 0;
const ids = {};
2018-12-13 21:26:21 +08:00
// Support call raf with delay specified frame
2019-01-12 11:33:27 +08:00
export default function wrapperRaf(callback, delayFrames = 1) {
const myId = id++;
let restFrames = delayFrames;
2018-12-13 21:26:21 +08:00
2019-01-12 11:33:27 +08:00
function internalCallback() {
restFrames -= 1;
2018-12-13 21:26:21 +08:00
if (restFrames <= 0) {
2019-01-12 11:33:27 +08:00
callback();
delete ids[id];
2018-12-13 21:26:21 +08:00
} else {
2019-01-12 11:33:27 +08:00
ids[id] = raf(internalCallback);
2018-12-13 21:26:21 +08:00
}
}
2019-01-12 11:33:27 +08:00
ids[id] = raf(internalCallback);
2018-12-13 21:26:21 +08:00
2019-01-12 11:33:27 +08:00
return myId;
2018-12-13 21:26:21 +08:00
}
wrapperRaf.cancel = function(pid) {
raf.cancel(ids[pid]);
delete ids[pid];
2019-01-12 11:33:27 +08:00
};