美文网首页
递归循环调用

递归循环调用

作者: wxw_威 | 来源:发表于2021-04-24 11:08 被阅读0次

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

    相关文章

      网友评论

          本文标题:递归循环调用

          本文链接:https://www.haomeiwen.com/subject/heikrltx.html