面试题:有一个url数组,var urls = [‘/a’, ‘/b’, ‘/c’,…],要求依次请求这几个url, 要求是 第一个请求完了才发第二个请求,第二个完了才发第三个请求,如此依次。
async doFuc(db) {
let urls = ['/a', '/b', '/c'];
for (let doc of urls) {
await db.post(doc);
}
}
面试题:定义这样一个函数
function repeat(func, times, wait) {},参数分别是需要 repeat的函数, repeat的次数,每次repeat的间隔,使用方式如下:调用这个函数能返回一个新函数,,比如传入的是alertvar repeatedFun = repeat(alert, 10, 5000);调用返回的这个新函数,如:repeatFun(“hellworld”);会alert十次 helloworld
function repeat(func, times, wait) {
if (!window.count) {
window.count = 0
}
if (times <= 0) {
return;
}
setTimeout(() => {
func()
times--;
count++
repeat(func, times, wait)
}, count ? wait : 0)
}
function repeat(func, times, wait) {
for (let i = 0; i < times; i++) {
(function(i) {
setTimeout((i) => {
func()
}, wait * i)
})(i)
}
}
async function repeat(func, times, wait) {
for (let i = 0; i < times; i++) {
await setTimeout(() => {
func()
}, wait * i)
}
}
function fun1() {
console.log(2, new Date())
}
repeat(fun1, 3, 1000)