|
@@ -0,0 +1,23 @@
|
|
|
+/**
|
|
|
+ * 执行动画函数
|
|
|
+ * @param {Function} callback 回调函数
|
|
|
+ * @param {boolean} isEnd 是否停止
|
|
|
+ * @param {number} duration 绘制的间隔时间(默认1s)
|
|
|
+ */
|
|
|
+export default function animation(callback, duration = 1000) {
|
|
|
+ // 判断是否为函数
|
|
|
+ if(typeof callback !== 'function') throw new Error('callback is not a function');
|
|
|
+ // 判断是否为数字
|
|
|
+ if(typeof duration !== 'number') throw new Error('duration is not a number');
|
|
|
+ // 判断是否 > 16
|
|
|
+ if(duration <= 16) throw new Error('duration is not a Less than or equal to 16');
|
|
|
+
|
|
|
+ let time = Date.now();
|
|
|
+ function _run() {
|
|
|
+ if (Date.now() - time > duration) {
|
|
|
+ time = Date.now();
|
|
|
+ callback(_run);
|
|
|
+ } else requestAnimationFrame(_run);
|
|
|
+ }
|
|
|
+ _run();
|
|
|
+}
|