FE-Interview icon indicating copy to clipboard operation
FE-Interview copied to clipboard

动手实现一个 repeat 方法

Open lgwebdream opened this issue 5 years ago • 12 comments
trafficstars

function repeat(func, times, wait) {
  // TODO
}
const repeatFunc = repeat(alert, 4, 3000);
// 调用这个 repeatFunc ("hellworld"),会alert4次 helloworld, 每次间隔3秒

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

async function sleep(fn, wait, args) {
  return new Promise((resolve) => {
    setTimeout(() => {
      fn.apply(this, args)
      resolve()
    }, wait)
  })
}
function repeat(func, times, wait) {
  return async function() {
    for (let i = 0; i < times; i++) {
      await sleep(func, wait, arguments)
    }
  }
}
var repeatFunc = repeat(alert, 4, 3000);
repeatFunc('helloworld')

523451928 avatar Jul 16 '20 09:07 523451928

function repeat(func, times, wait) {
    function sleep(fn, wait, args) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    const res = typeof fn === 'function' && fn.apply(this, args);
                    resolve(res);
                } catch (error) {
                    reject(error);
                }
            }, wait);
        });
    }
    // TODO
    return async function (...args) {
        const promises = new Array(times).fill(sleep);
        for (const p of promises) {
            await p(func, wait, args);
        }
    }

}
const repeatFunc = repeat(console.log, 4, 3000);
repeatFunc ("hellworld");

GolderBrother avatar Aug 30 '20 09:08 GolderBrother

function repeat(func, times, wait = 1000) {
    // TODO
    return async function (...args) {
        function delay(fn, wait) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    try {
                        const res = typeof fn === 'function' && fn.apply(this, args);
                        resolve(res);
                    } catch (error) {
                        reject(error);
                    }
                }, wait);
            });
        }
        while (times > 0) {
            await delay(func, wait);
            times--;
        }
        typeof cb === 'function' && cb.apply(this, args);
    }
}
const repeatFunc = repeat(console.log, 4, 3000);
repeatFunc('hellworld');

GolderBrother avatar Sep 06 '20 10:09 GolderBrother

function repeat(func, times, wait) {
    return function (...args) {
        let that = this
        let count = 0
        let result
        let handler = setInterval(() => {
            if (count >= times) {
                clearInterval(handler)
                return
            }

            result = func.apply(that, args)
            count++
        }, wait);

        return result
    }
}

yulishuta avatar Feb 15 '21 08:02 yulishuta

const repeat = (cb, times, interval) => (value) => {
  let count = 0
  const execution = () => {
    setTimeout(() => {
      count++
      cb(value)
      if (count < times) execution()
    }, interval)
  }
  execution()
}

repeat(console.log, 5, 200)("hi")

HW2821 avatar Sep 13 '21 06:09 HW2821

const repeatFunc = repeact(console.log,4,3000)
repeatFunc('helloword')

function repeact(cd, timers, interval) {
    let t = null; 
    let m = 0
    return function (values) {
        t = setInterval(() => {
            m++
            if(m > timers) {
                clearInterval(t)
            }else {
                cd(values)
            }
        }, interval);
    }
}

yangfan-coder avatar Dec 24 '21 02:12 yangfan-coder

function repeat(func, times, wait) {
  let counter = 0;
  // TODO
  return function (args) {
    
    // 每一次要做的事情
    function batch() {
      func(args);
      counter++;
      doBatch();
    }

    function doBatch() {
      if (counter < times) {
        sleep(batch, wait);
      }
    }

    function sleep(fn, wait) {
      setTimeout(fn, wait);
    }

    doBatch();

  }
}

Neisun avatar Jan 02 '22 08:01 Neisun

    function repeat(func, times, wait) {
      let c = 0
      let timer = null
      return function repeated(...args) {
        func.call(this, ...args)
        c++
        timer = setInterval(() => {
          func.call(this, ...args)
          c++
          if (c === times) {
            clearInterval(timer)
            timer = null
          }
        }, wait)
      }
    }

AAA611 avatar Aug 25 '22 09:08 AAA611


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

function repeat(func, times, wait) {
  return async function wrappedFunction(...args) {
    for (let index = 0; index < times; index++) {
      await sleep(wait);
      func.call(this, ...args);
    }
  };
}
const repeatFunc = repeat(console.log, 4, 3000);
repeatFunc("hellworld");

gaohan1994 avatar Apr 28 '24 08:04 gaohan1994