animation.js 809 B

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