All files / fn setQueue.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                          1x 1x 1x 4x 2x   2x       1x   1x 1x   2x             1x  
/**
 * 包装为一个队列,按设置的时间间隔触发任务函数
 * - 插入队列的所有函数都会执行,但每次执行之间都会有一个固定的时间间隔。
 * @method setQueue
 * @param {Function} fn 要延迟触发的函数
 * @param {Number} delay 延迟时间(ms)
 * @param {Object} [bind] 函数的 this 指向
 * @returns {Function} 经过包装的队列触发函数
 * @example
 * import { setQueue } from '@spore-ui/tskit';
 * const t1 = Date.now();
 * const doSomthing = setQueue((index) => {
 *   console.info(index + ':' + (Date.now() - t1));
 * }, 200);
 * // 每隔200ms输出一个日志。
 * for(let i = 0; i < 10; i++){
 *   doSomthing(i);
 * }
 */
import { TypeInterval } from '../types';
 
export function setQueue(fn: Function, delay: number): Function {
  let timer: TypeInterval = null;
  const arr: Function[] = [];
  return (...args: unknown[]) => {
    arr.push(() => {
      if (typeof fn === 'function') {
        fn(...args);
      }
    });
    if (!timer) {
      timer = setInterval(() => {
        if (!arr.length) {
          clearInterval(timer);
          timer = null;
        } else {
          arr.shift()();
        }
      }, delay);
    }
  };
}
 
export default setQueue;