vue - uniapp定时刷新

用于定时请求接口或者刷新页面的操作

// 首先调用一次函数否则就要等5秒后才能执行
this.getData();
let timer = setInterval(() => {
    // 使用setTimeout是因为它可以清除定时器
    setTimeout(this.getData, 0)
}, 5000);

// 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
this.$once('hook:beforeDestroy', () => {            
    clearInterval(timer);                                    
});
posted @ 2021-10-18 16:13  大海&  阅读(1697)  评论(0编辑  收藏  举报