受chrome机制影响,熄屏、最小化等条件下会降低 setInterval 的频率,聚焦后恢复正常。所以获取程序运行时间应换一种方式实现,获取电脑开机时间,分别记录程序启动时间和当前开机时间,然后相减可得程序当前运行时间。
// 运行时间定时器
handleCheckUptime({ state, commit }) {
const os = require('os');
commit('SET_UPTIME_TIMER', { start: os.uptime() });
clearInterval(state.game_running.check_uptime_timer);
let timer = setInterval(() => {
let diff = state.game_running.end - state.game_running.start;
if (diff < 0) { diff = 0 }
commit('SET_UPTIME_TIMER', { end: os.uptime(), diff });
console.log('[ uptime ] >', state.game_running.start, state.game_running.end, state.game_running.diff);
}, 1000);
commit('SET_UPTIME_TIMER', { check_uptime_timer: timer })
}