raf.js 719 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.raf = raf;
  4. exports.cancel = cancel;
  5. var _index = require("./index");
  6. /**
  7. * requestAnimationFrame polyfill
  8. */
  9. var prev = Date.now();
  10. /* istanbul ignore next */
  11. function fallback(fn) {
  12. var curr = Date.now();
  13. var ms = Math.max(0, 16 - (curr - prev));
  14. var id = setTimeout(fn, ms);
  15. prev = curr + ms;
  16. return id;
  17. }
  18. /* istanbul ignore next */
  19. var root = _index.isServer ? global : window;
  20. /* istanbul ignore next */
  21. var iRaf = root.requestAnimationFrame || fallback;
  22. /* istanbul ignore next */
  23. var iCancel = root.cancelAnimationFrame || root.clearTimeout;
  24. function raf(fn) {
  25. return iRaf.call(root, fn);
  26. }
  27. function cancel(id) {
  28. iCancel.call(root, id);
  29. }