需求的场景描述(希望解决的问题)
调用手机姿态api时,promise出错
- 希望提供的能力
设备状态改变100后,重新开始计数。
实际验证时,changeItem在100次后,调用了wx.stopDeviceMotionListening,但弹出警告框病点击确定后,有一个changeItem从0开始,但还存在一个101,就是还存在一个changItem并没有置0,接着100往后计数。 请问是promise,then用错了吗?
getReady() { var that = this let timeStart = new Date().getTime(); that.ff2(timeStart).then( function (va){ console.log(va) }, function (re){ //如果超过100次,重新调整 console.log(re) // that.getReady() wx.showModal({ title: 'error' , content: 'try again' , success(res){ if (res.confirm){ that.getReady() } else if (res.cancel){ console.log( 'cancel' ) } } }) }) } ff2(timeStart){ let showNum = 0 let posiResult; let p1 = new Promise( function (resolve, reject){ if (wx.startDeviceMotionListening){ wx.startDeviceMotionListening({ interval: 'normal' , success: function (res){ console.log( 'start motion' ) console.log(res); //let showNum = 0 let changeItem = 0; console.log( 'at beginning changeItem is ' , changeItem) wx.onDeviceMotionChange( function (res){ changeItem++ console.log(changeItem) if (changeItem >= 100){ wx.stopDeviceMotionListening({ fail: function (res){ console.log( 'stop function failed ' , res) }, success: function (res){ console.log( "stop device motion listening" ) console.log( "changeItem is " + changeItem) posiResult = false console.log( 'error, changeItem is ' , changeItem) reject(posiResult) } }) } }) }, fail: function (res){ posiResult = false reject(posiResult) } }) } }) return p1 } |