throttleByAnimationFrame.js 556 B

12345678910111213141516171819202122
  1. import raf from './raf';
  2. function throttleByAnimationFrame(fn) {
  3. let requestId;
  4. const later = args => () => {
  5. requestId = null;
  6. fn(...args);
  7. };
  8. const throttled = function () {
  9. if (requestId == null) {
  10. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  11. args[_key] = arguments[_key];
  12. }
  13. requestId = raf(later(args));
  14. }
  15. };
  16. throttled.cancel = () => {
  17. raf.cancel(requestId);
  18. requestId = null;
  19. };
  20. return throttled;
  21. }
  22. export default throttleByAnimationFrame;