美文网首页
每隔wait秒执行函数,一共执行times次

每隔wait秒执行函数,一共执行times次

作者: peerben | 来源:发表于2019-05-31 07:27 被阅读0次

实现一个函数,每隔wait秒执行函数,一共执行times次。

给出两种实现

export {}

function executeEveryTime(wait: number, times: number, fn: Function) {
  if (times === 0) return;

  setTimeout(() => {
    fn.call(null);
    
    executeEveryTime(wait, times-1, fn);
  }, wait);
}

const delay = (wait) => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, wait);
  })
}

async function executeEvery(wait: number, times: number, fn: Function) {

  while (times > 0) {

    await delay(wait);
    fn.call(null);

    times -= 1;
  }

}

const log = () => console.log('hello world');

//executeEveryTime(1000, 3, log);
executeEvery(1000, 4, log);
Screen Shot 2019-05-31 at 7.26.45 AM.png

相关文章

  • 每隔wait秒执行函数,一共执行times次

    实现一个函数,每隔wait秒执行函数,一共执行times次。 给出两种实现

  • Handle 定时任务

    每隔100毫秒执行一次

  • cron表达式

    Cron表达式范例: */5 * * * * ? 每隔5秒执行一次 0 */1 * * * ? 每隔1分钟执行一次...

  • 定时任务

    定时任务 每秒执行一次 每隔30秒执行一次 每次10分钟执行一次 //每秒都执行 每次10分钟执行一次 //0秒的...

  • 常用的cron表达式

    Cron表达式范例: */5 * * * * ? 每隔5秒执行一次0 */1 * * * ? 每隔1分钟执行一...

  • gocron定时任务

    安装: 每隔1秒执行一个任务,每隔4秒执行另一个任务:

  • 防抖函数

    返回function函数防反跳版本,将延迟函数执行(真正的执行)在函数最后一次调用时候的wait毫秒后。 传参im...

  • JS-计时器

    (1)setInterval(fn, time) -- 周期性执行,每隔 time 时长执行一次 fn 函数 (2...

  • Cron表达式

    常用的: */5 * * * * ? 每5秒执行一次 0 */1 * * * ? 每隔1分钟执行一次 0 0/5 ...

  • [underscore 源码学习] delay 延迟执行函数 -

    延迟执行函数 delay _.delay(function, wait, *arguments) 类似 setTi...

网友评论

      本文标题:每隔wait秒执行函数,一共执行times次

      本文链接:https://www.haomeiwen.com/subject/zayitctx.html