Skip to content

repeat

实现一个 repeat 方法,要求如下:

js
// 实现 repat 方法, 使 func 方法重复执行 times 次, 每次间隔 wait 秒
function repeat(func, times, wait) {
  // empty
}

const sayHello = (name) => {
  console.log(`hello, ${name}`);
};

const repeatFunc = repeat(sayHello, 4, 1000);
repeatFunc("kevin");
js
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

const repeat = (func, times, wait) => {
  return async (...args) => {
    for (let i = 0; i < times; i++) {
      func(...args);
      await sleep(wait);
    }
  };
};
js
const repeat = (func, times, wait) => {
  let num = 0;
  return (args) => {
    function run() {
      func(args);
      num++;
      if (num < times) {
        setTimeout(run, wait);
      }
    }

    run();
  };
};
2025( )
今日 8.33%
本周 42.86%
本月 48.39%
本年 4.11%
Powered by Snowinlu | Copyright © 2024- | MIT License