js多次调用接口,全部成功之后执行
handleDownload() {
this.count = 3
this.imgList = []
this.addImgUrl()
},
addImgUrl() {
if(this.count > 0) {
this.getImgUrl().then((res) => {
this.imgList.push(res)
this.count--
this.addImgUrl()
})
} else {
console.log('imgList', this.imgList)
}
},
getImgUrl() {
return new Promise((resolve, reject) => {
let timeValue = Math.floor(Math.random()*5+1)
console.log(timeValue)
setTimeout(() => {
let url = `url${timeValue}`
console.log(url)
resolve(url)
}, timeValue * 1000)
})
}
输出
5
url5
3
url3
4
url4
imgList (3)["url5", "url3", "url4", __ob__: Observer]
多层数组嵌套循环输出
let arr = [
{name: 'aaa', children:[ {name: 'bbb', children: [{name: 'cccc'}]}]},
{name: 'ddd', children:[{name: 'rrr', children: [{name: 'fff'}]}]},
{name: 'asdf'}
]
// 遍历循环
func1() {
this.getDateForSelectAll(arr)
},
getDateForSelectAll(arr) {
arr.map((item) => {
console.log(item.name)
if (item.children && item.children.length > 0) {
this.getDateForSelectAll(item.children)
}
})
},
网友评论