SetInterval and background mode


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 ?


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.


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(() => {
        }, 1000);
        this.$store.commit("setTimerId", intervalId);

Inside Meteor.startup:

document.addEventListener("resume", function () {

				const intervalId = Meteor.setInterval(function () {
				}, 1000);
				store.commit("setTimerId", intervalId); 
		document.addEventListener("pause", function () {
				Meteor.clearInterval(store.state.timerId); // stop the Timer i.e clear the interval


// 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) {
1 Like