前端大屏有个需求逻辑,每天的凌时刷新一次数据,,于是就想到了使用计时器,拿到明天零点的时间,获取当前的时间,两个时间戳相减获取的毫秒作为计时器的倒计时。
// 计时器 = 明天凌晨 + 10s刷新
const SetInterval = () => {
const Time = Number(new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate() + 1, 0, 0, 10)) - Number(new Date())//明天零点 + 10s 时间戳 - 当前时间的时间戳 = 计时器倒计时毫秒
timer.value = setInterval(() => {
getPlantFactorData();
}, Time)
}
在进入界面后立即调用函数,每次进入界面都会重新刷新倒计时的时间。
1.写在onMounted调用函数SetInterval(),
2.记得离开界面后清除计时器window.clearInterval(timer.value)
3.加10s刷新是怕后端数据生成延迟问题,可以跟后端讨论