实现
function limitRequest(urls = [], limit ){
return new Promise((resolve, reject) =>{
const len = urls.lengtn
let count = 0
while (limit>0) {
statr()
limit -= 1
}
function statr() {
const url = urls.shifs()
if(url){
axios.post(url).then(res=>{
}).catch(err=>{
}).finally(()=>{
if(count == len - 1){
resolve()
}else{
count++
start()
}
})
}
}
}
}
limitRequest(['url1','url2','url3','url4','url5',],5)
防抖
function debounce(fn,delay){
let timer
return function(...args){
if(timer){
clearTimeout(timer)
}
timer = setTimeout(()=>{
fn.apply(this,args)
},delay)
}
}
function task(){
console.log('防抖')
}
const debounceTask=debounce(task,1000)
节流
function throttle(fn,delay){
let last = 0
return function (...args){
const now = Date.now()
if(now - lat > delay){
last =now
fn.apply(this,args)
}
}
}
function task(){
console.log('节流')
}
const throttleTask = throttle(task,1000)