在做项目中经常要用到定时器,过多久执行什么方法,我这是做页面提示,当用户请求成功或失败时,给用户反馈,但是我们总不能让反馈消息一直显示在页面上,这样体验感也太难受了,所以我这里是过2秒就自动把提示清除
setTimeout(() => {handler }, time);在{handler}中执行你的方法,time是过多久执行
readCollect() { this.hasDeviceId = false; this.sendSuccess = false; if (this.deviceId !== null) { console.log('readCollect: ' + this.deviceId); this.command = 'MCU32 ' + this.deviceId + ' Read'; this.orderinfoService.sendCommand(this.deviceId, this.command).subscribe(result => { console.log('result:' + result); this.sendSuccess = true; this.reply = '读取采集结果' + result; setTimeout(() => {this.sendSuccess = false; }, 2000); }); } else { this.hasDeviceId = true; setTimeout(() => {this.hasDeviceId = false; }, 2000); } }