- 同样的程序,安卓手机不好使但是开发者工具,苹果都好使
一个控制蓝牙小灯的程序,在ios上和开发者工具里面都能正常的延时,但是就是安卓手机不可以。这是什么情况。 出问题的地方就是那个timer函数,安卓手机并没有执行延时而是直接跳过去了,进行下一步的指令了。 var app = getApp(); var display = new Array(); var inst = new Array(); var value = new Array(); var index = 0; var inputValue = ''; var inputInst = ''; var current = 0; var x = 0; let buffer = new ArrayBuffer(20) let dataView = new DataView(buffer) dataView.setUint8(0, 0) dataView.setUint8(1, 0) dataView.setUint8(2, 0) dataView.setUint8(3, 0) dataView.setUint8(4, 0) dataView.setUint8(5, 0) dataView.setUint8(6, 0) dataView.setUint8(7, 0) dataView.setUint8(8, 0) dataView.setUint8(9, 0) dataView.setUint8(10, 0) dataView.setUint8(11, 0) dataView.setUint8(12, 0) dataView.setUint8(13, 0) dataView.setUint8(14, 0) dataView.setUint8(15, 0) dataView.setUint8(16, 0) dataView.setUint8(17, 0) dataView.setUint8(18, 0) dataView.setUint8(19, 0) Page({ data: { instructions: ['关闭', '开启', '延时'], }, onUnload: function () { wx.closeBLEConnection({ deviceId: app.globalData.targetAdd, success: function (res) { app.globalData.connectFlag = false, app.globalData.searchFlag = false, app.globalData.targetAdd = 'NoAdd', console.log(res) console.log('app.globalData.connectFlag', app.globalData.connectFlag) console.log('app.globalData.searchFlag', app.globalData.searchFlag) console.log('app.globalData.targetAdd', app.globalData.targetAdd) } }) }, bindPickerChange: function (e) { console.log('picker发送选择改变,携带值为', e.detail.value) if (e.detail.value == 0) { inputInst = '关闭'; } if (e.detail.value == 1) { inputInst = '开启'; } if (e.detail.value == 2) { inputInst = '延时'; } this.setData({ index: e.detail.value }) console.log('指令值: ', inputInst) }, inputDone: function (e) { inputValue = e.detail.value }, inputInstructions: function () { inst.push(inputInst) console.log('指令数组', inst) value.push(inputValue) console.log('值数组', value) display.push(inputInst + ": " + inputValue) console.log('显示数组', display) this.setData({ display: display }) }, deleteInstructions: function () { inst.splice(inst.length - 1, 1) value.splice(value.length - 1, 1) display.splice(display.length - 1, 1) this.setData({ display: display }) }, run: function () { if (current < inst.length) { switch (inst[current]) { case '开启': this.runStart(); break; case '关闭': this.runStop(); break; case '延时': this.runDelay(); break; } } else { current = 0 } }, runStart: function () { dataView.setUint8(value[current] - 1, 1) wx.writeBLECharacteristicValue({ deviceId: app.globalData.targetAdd, serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB', characteristicId: '0000FFF6-0000-1000-8000-00805F9B34FB', value: buffer, success: function (res) { console.log('writeBLECharacteristicValue success', res) }, fail: function (res) { console.log("wrong", res) } }) console.log(inst[current], value[current], current) current++; this.run(); }, runStop: function () { dataView.setUint8(value[current] - 1, 0) wx.writeBLECharacteristicValue({ deviceId: app.globalData.targetAdd, serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB', characteristicId: '0000FFF6-0000-1000-8000-00805F9B34FB', value: buffer, success: function (res) { console.log('writeBLECharacteristicValue success', res) }, fail: function (res) { console.log("wrong", res) } }) console.log(inst[current], value[current], current) current++; this.run(); }, runDelay: function () { console.log(inst[current], value[current], current) this.timer() // if (x < value[current]) { // this.timer(); // } else { // x = 0 // current++ // this.run() // } }, timer: function () { const that = this setTimeout(function () { // x++; // console.log('x', x) current++ that.run() }, value[current]) } })
2018-07-30 - 微信小程序延时问题
- 需求的场景描述(希望解决的问题) 用户会输入指令,小程序自动执行,控制连接的蓝牙设备开启关闭,需要一个延时的功能 - 希望提供的能力 希望有C中的delay 变量的延时功能,就是想让小程序停止一段时间(变量输入的时间)。
2018-07-23