animation.js 772 B

1234567891011121314151617181920212223
  1. /**
  2. * 执行动画函数
  3. * @param {Function} callback 回调函数
  4. * @param {boolean} isEnd 是否停止
  5. * @param {number} duration 绘制的间隔时间(默认1s)
  6. */
  7. export default function animation(callback, duration = 1000) {
  8. // 判断是否为函数
  9. if(typeof callback !== 'function') throw new Error('callback is not a function');
  10. // 判断是否为数字
  11. if(typeof duration !== 'number') throw new Error('duration is not a number');
  12. // 判断是否 > 16
  13. if(duration <= 16) throw new Error('duration is not a Less than or equal to 16');
  14. let time = Date.now();
  15. function _run() {
  16. if (Date.now() - time > duration) {
  17. time = Date.now();
  18. callback(_run);
  19. } else requestAnimationFrame(_run);
  20. }
  21. _run();
  22. }