关于前端:每隔10秒轮询接口

场景:
1、数据量宏大
2、每隔几秒申请一次接口刷新数据

在查问数据的时候,因为参数和数据量宏大,在查询数据库可能返回数据较慢,导致接口超时报错等,因而须要轮询的形式,每隔10秒去查问接口。

办法:setIntervalsetTimeout

// 查问
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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理