Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

动手实现一个 repeat 方法 #148

Open
lgwebdream opened this issue Jul 6, 2020 · 11 comments
Open

动手实现一个 repeat 方法 #148

lgwebdream opened this issue Jul 6, 2020 · 11 comments
Labels
JavaScript teach_tag 头条 company 编程题 teach_tag

Comments

@lgwebdream
Copy link
Owner

function repeat(func, times, wait) {
  // TODO
}
const repeatFunc = repeat(alert, 4, 3000);
// 调用这个 repeatFunc ("hellworld"),会alert4次 helloworld, 每次间隔3秒
@lgwebdream lgwebdream added JavaScript teach_tag 头条 company 编程题 teach_tag labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

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

@523451928
Copy link

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')

@GolderBrother
Copy link

GolderBrother commented Aug 30, 2020

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
Copy link

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');

@yulishuta
Copy link

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
    }
}

@HW2821
Copy link

HW2821 commented Sep 13, 2021

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")

@yangfan-coder
Copy link

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);
    }
}

@Neisun
Copy link

Neisun commented Jan 2, 2022

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();

  }
}

@AAA611
Copy link

AAA611 commented Aug 25, 2022

    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)
      }
    }

@gaohan1994
Copy link

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");

@wudizhuzhumei
Copy link

      function repeat(func, times, wait) {
            let timer  = null;
            return function fn(...args){
                timer = setTimeout(() => {
                    func.apply(this, args);
                    times--;
                    if(times > 0){
                        fn(...args);
                    }
                }, wait)
            }
        } 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 头条 company 编程题 teach_tag
Projects
None yet
Development

No branches or pull requests

10 participants