setRafTimeout.js 583 B

12345678910111213141516171819
  1. function safeRequestAnimationFrame(callback) {
  2. if (typeof requestAnimationFrame !== 'undefined') requestAnimationFrame(callback);
  3. }
  4. export default function setRafTimeout(callback) {
  5. var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  6. var currTime = -1;
  7. var shouldUpdate = function shouldUpdate(now) {
  8. if (currTime < 0) {
  9. currTime = now;
  10. }
  11. if (now - currTime > timeout) {
  12. callback(now);
  13. currTime = -1;
  14. } else {
  15. safeRequestAnimationFrame(shouldUpdate);
  16. }
  17. };
  18. requestAnimationFrame(shouldUpdate);
  19. }