Vue 设置等待时间执行for循环

发送异步请求
async executewirelessscene(item) {
      let _this = this;
      let devices = _this.currentdeviceList;
      let Scenenumber = item.Slot;
      // console.log(Scenenumber);
      // console.log(devices.length);
      for (var i = 0, length = devices.length; i < length; i++) {
        var dName = devices[i].deviceName;
        var dzigbeeid = devices[i].zigbeeid;
        var dgroupid = devices[i].groupid;
        const param = {
          type: dgroupid,
          gatewayId: _this.gatewayID,
          zigbeeId: dzigbeeid,
          value: Scenenumber
        };
        const result = await this.post(
          "/executewirelessscene",
          JSON.stringify(param)
        );
        if (result.success) {
          this.$message({
            message: dName + "指令下发成功",
            type: "success"
          });
        } else {
          this.$message({
            message: "failed!",
            type: "warning"
          });
        }
        this.sleep(500);//for循环等待500ms
      }
    },

请求等待函数

//请求等待函数
    async sleep(n) {
        var start = new Date().getTime();
        while (true) {
            if (new Date().getTime() - start > n) {
                break;
            }
        }
    },

 

posted on 2021-08-23 16:34  不酷也要写代码  阅读(3050)  评论(0编辑  收藏  举报

导航