requestAnimationTimeout.js 584 B

123456789101112131415161718
  1. import getRequestAnimationFrame, { cancelRequestAnimationFrame as caf } from './getRequestAnimationFrame';
  2. const raf = getRequestAnimationFrame();
  3. export const cancelAnimationTimeout = frame => caf(frame.id);
  4. export const requestAnimationTimeout = function (callback) {
  5. let delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  6. const start = Date.now();
  7. function timeout() {
  8. if (Date.now() - start >= delay) {
  9. callback.call();
  10. } else {
  11. frame.id = raf(timeout);
  12. }
  13. }
  14. const frame = {
  15. id: raf(timeout)
  16. };
  17. return frame;
  18. };