场景:
1、数据量宏大
2、每隔几秒申请一次接口刷新数据
在查问数据的时候,因为参数和数据量宏大,在查询数据库可能返回数据较慢,导致接口超时报错等,因而须要轮询的形式,每隔10秒去查问接口。
办法:setInterval
、setTimeout
// 查问search() { const delayTime = 60 * 1000 let res = this.getData() if (res === 0) { // 革除定时器 clearInterval(this.timer) console.log('查问胜利') } else { console.log('查问超时') // 革除定时器 clearInterval(this.timer) this.timer = setInterval(() => { this.search() }, delayTime) }},// 查问数据getData() {}
留神:setInterval须要革除定时器队列,每反复执行1次都会导致定时器叠加,在销毁周期destroyed也要革除setInterval