Hi,
I am currently developping an application using Vue + Meteor + Cordova.
I want to implement a timer and for that, i am using Meteor.setInterval to do it.
The problem is when the application goes in background or the mobile is locked, the timer is stopped after a period of time and it starts again when the application goes foreground.
Precision: the application is removed from the automatic battery management system.
Is it right way to implement a timer with meteor/cordova ?
Thanks
1 Like
I use this approach to iterate over a large array without blocking the UI. You might possibly use a similar approach to check system time every X milliseconds and then fire a callback after the appropriate interval. Just a thought!
I use resume and pause events to update my timer and manage setInterval.
Thanks.
That sounds interesting, @harry73. Could you post some code or some pseudo-code by any chance?
I am using Vuex to store Timer states.
Inside a component:
const intervalId = Meteor.setInterval(() => {
this.$store.commit("updateTimerRecord");
}, 1000);
this.$store.commit("setTimerId", intervalId);
Inside Meteor.startup:
document.addEventListener("resume", function () {
store.commit("updateTimerRecordonResume");
const intervalId = Meteor.setInterval(function () {
store.commit("updateTimerRecord");
}, 1000);
store.commit("setTimerId", intervalId);
}
});
document.addEventListener("pause", function () {
store.commit("setStartTimeRecord");
Meteor.clearInterval(store.state.timerId); // stop the Timer i.e clear the interval
});
vuex.js
// calculate time between pause and resume event and update the Timer
updateTimerRecordonResume(state) {
let temp = moment().utc() - state.startTimeRecord;
state.timerRecord += Math.floor(temp / 1000);
},
// store the date/time when the app goes background
setStartTimeRecord(state) {
state.startTimeRecord = moment().utc();
},
// store the Timer/Interval Id
setTimerId(state, id) {
state.timerId = id;
},
//
updateTimerRecord(state) {
state.timerRecord++;
},
1 Like