throttleByAnimationFrame.js 541 B

1234567891011121314151617181920
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import raf from "rc-util/es/raf";
  3. function throttleByAnimationFrame(fn) {
  4. let requestId = null;
  5. const later = args => () => {
  6. requestId = null;
  7. fn.apply(void 0, _toConsumableArray(args));
  8. };
  9. const throttled = (...args) => {
  10. if (requestId === null) {
  11. requestId = raf(later(args));
  12. }
  13. };
  14. throttled.cancel = () => {
  15. raf.cancel(requestId);
  16. requestId = null;
  17. };
  18. return throttled;
  19. }
  20. export default throttleByAnimationFrame;