new Promise(resolve => { console.log('promise'); resolve(5); }).then(value=>{ console.log('the...

这段代码的执行结果为:

  1. 输出 "promise"
  2. 调用 func1,输出 "func1"
  3. 设置了一个 setTimeout 函数,在一段时间后,输出 "setTimeout"
  4. Promise 的 resolve 函数被调用,传入值为 5,输出 "then回调 5"

注意:setTimeout 是异步执行的,所以不能确定它具体在什么时候输出,但是它一定是在 func1 之后执行。

修改这段代码,使第一个定时任务先执行,第一个定时任务执行完之后,开始执行第二个定时任务,并且第二个定时任务执行时,第一个定时任务不再开启。 findBlue(){ var _this = this; wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success:(res) => { console.log("搜索蓝牙设备成功...") setTimeout(function(){ _this.getBlue() },500) }, fail: (res) => { console.log('搜索附近的蓝牙设备失败') console.log(res) } }) }, // 3.搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息 getBlue(){ var _this = this; wx.getBluetoothDevices({ success: (res) => { console.log(res) if(res.devices.length > 0){ for (var i = 0; i < res.devices.length; i++){ // 判断里面是否有我想要的蓝牙设备 if (res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue){ console.log("name:"+res.devices[i].name) this.setData({ isFound: true, deviceId: res.devices[i].deviceId }) //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log('扫描到目标设备之后关闭蓝牙搜索'); setTimeout(function(){ _this.connetBlue(_this.data.deviceId);//4.0 },500) } }) // this.connetBlue(res.devices[i].deviceId);//4.0 return; } } } console.log("重新找....") var time = setTimeout(function(){ if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("执行任务1==================") _this.findBlue() } },500) setTimeout(function(){ console.log("执行任务2---------------------") clearTimeout(time) console.log("清除time定时任务") console.log("isFound:",_this.data.isFound) console.log("isTui:",_this.data.isTui) console.log("isTip:",_this.data.isTip) if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("重新打开适配器") wx.openBluetoothAdapter({ success: (res) => { console.log('初始化蓝牙设备成功') //获取适配器状态 wx.getBluetoothAdapterState({ success(res) { setTimeout(function(){ _this.findBlue();//2.0 },800) } }) } }) } }, 3000) }, fail: () =>{ console.log("搜索蓝牙设备失败") } }) },
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值