业务场景:某个业务号经过缴费之后,会生成一个保单号,我们需要每隔几秒请求一次拿到保单号

在此场景中,因为保单号是需要第三方平台确认收到账款之后才返回的,而且是一个一个单号返回的,当我们选择n条数据进行操作的时候,我们就必须每隔几秒请求一次,直到我们拿到的保单号和我们请求的数据条数相同时,停止请求

话不多说,直接上代码:
使用的是vue+axios
假设 获取保单号的后端接口为 cashierStatusNotice
参数为 流水号 tradeNo

//下面的写在created生命周期中

let timer
cashierStatusNotice(this.tradeNo).then(res => {
        console.log(res)
        if (res.code === '200') {
          // 保单号的条数
          this.tradeTotalCount = res.data.tradeTotalCount
          // res.data.tradeList 后端返回的保单号集合
          if (res.data.tradeList) {
            this.policyNoS = res.data.tradeList
          }
          console.log(this.policyNoS)
          if (!this.policyNo && this.tradeTotalCount == res.data.tradeList.length) {
            // 如果获取到保单号并且保单号和我们请求的条数相等时,我们清楚定时器,不让他再请求
            clearTimeout(timer) // 清理定时任务
          } else {
          //否则的话,每隔一秒钟请求一次
            timer = setTimeout(() => {
              this.getInit()
            }, 1000)
          }
        }
      }).catch(e => {
        console.log(e)
      })

创作不易,谢谢各位看官,非常感谢,希望可以帮助到你们!!!

Logo

前往低代码交流专区

更多推荐