FE-Interview
FE-Interview copied to clipboard
动手实现一个 repeat 方法
trafficstars
function repeat(func, times, wait) {
// TODO
}
const repeatFunc = repeat(alert, 4, 3000);
// 调用这个 repeatFunc ("hellworld"),会alert4次 helloworld, 每次间隔3秒
扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。
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')
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");
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');
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
}
}
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")
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);
}
}
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();
}
}
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)
}
}
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");