Skip to content

Latest commit

 

History

History
84 lines (68 loc) · 1.54 KB

repeat.md

File metadata and controls

84 lines (68 loc) · 1.54 KB

repeat重复执行

描述

定义repeat(func, times, wait)函数,实现如下效果。

function repeat(func, times, wait) {
  // TODO
}
const repeatFunc = repeat(console.log, 4, 3000);
repeatFunc ("helloworld"); // 每隔3秒打印一句 'helloworld' 总共重复4次

实现

sleep + async函数

还记得sleep函数吗?可以使用sleep函数实现。

function sleep(func, delay) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(func());
    }, delay);
  });
}

function repeat(func, times, wait) {
  return async function(...arr) {
    for (let i = 0; i < times; i++) {
      await sleep(() => {
        func(...arr);
      }, wait);
    }
  }
}

Promise递归调用

思路:使用Promise去调用函数(本质是就是sleep函数),如果次数少于times则递归调用。

function repeat(func, times, wait) {
  let t = 0;
  let fn = async (...arr) => {
    await new Promise((resolve) => {  
      setTimeout(() => {
        resolve(func(...arr));
      }, wait);
    });

    t++;

    if(t < times) {
      fn(...arr);
    }
  }
  return fn;
}

setInterval延迟执行

思路:使用setInterval执行,如果次数超过times则取消定时器。

function repeat(func, times, wait) {
  let id = null; 
  let t = 0;
  return function (...arr) {
    id = setInterval(() => {
      t++;
      if(t > times) {
        clearInterval(id);
      } else {
        func(...arr);
      }
    }, wait);
  }
}