react-native-background-timer icon indicating copy to clipboard operation
react-native-background-timer copied to clipboard

BackgroundTimer.setInterval is not synchronized with the actual timer

Open scrapecoder opened this issue 4 years ago • 10 comments

BackgroundTimer.setInterval() is running 4-5 seconds more than the actual timer. when I try to run this for 5 minutes it's actually running for 5 minutes 5 seconds. which is not good

  x = BackgroundTimer.setInterval(() => {
		let { eventDate } = state;

		if (eventDate <= 0) {
			clearInterval(x);
		}
		else {
			eventDate = eventDate.subtract(1, 's');

			// const hours = eventDate.hours();
			// const mins = eventDate.minutes();
			// const secs = eventDate.seconds();

			setState({
				// hours,

				eventDate
			});
		}
	}, 1000);

scrapecoder avatar Sep 20 '20 11:09 scrapecoder

Same problem when i use BackgroundTimer.setInterval function.

gkhngyk avatar Sep 21 '20 11:09 gkhngyk

same issue here

tisch7 avatar Oct 05 '20 14:10 tisch7

Maybe this can help you: https://aloukissas.medium.com/how-to-build-a-background-timer-in-expo-react-native-without-ejecting-ea7d67478408 You can update the eventDate in the function by substracting the difference between the start date and now.

This is how I used it in my timer:

await AsyncStorage.setItem('start_time', new Date().toISOString());

BackgroundTimer.runBackgroundTimer(async () => {
      const startTime = await AsyncStorage.getItem('start_time');

      this.setState((prevState) => ({
        timeLeft:
          this.duration -
          differenceInSeconds(new Date(), Date.parse(startTime)),
      }));
    }, 1000);
  }

jnjacobson avatar Dec 22 '20 19:12 jnjacobson

@jnjacobson - thanks! this was the solution for me. timer is accurate now and not seemingly random

biddano avatar Feb 07 '21 01:02 biddano

@jnjacobson perfect!

OmarBasem avatar Jul 11 '21 14:07 OmarBasem

And btw, @jnjacobson solution does not even need BackgroundTimer, setInterval would do

OmarBasem avatar Jul 11 '21 14:07 OmarBasem

@jnjacobson thanks! That really helped!

maxim-green avatar Apr 12 '22 09:04 maxim-green

Maybe this can help you: https://aloukissas.medium.com/how-to-build-a-background-timer-in-expo-react-native-without-ejecting-ea7d67478408 You can update the eventDate in the function by substracting the difference between the start date and now.

This is how I used it in my timer:

await AsyncStorage.setItem('start_time', new Date().toISOString());

BackgroundTimer.runBackgroundTimer(async () => {
      const startTime = await AsyncStorage.getItem('start_time');

      this.setState((prevState) => ({
        timeLeft:
          this.duration -
          differenceInSeconds(new Date(), Date.parse(startTime)),
      }));
    }, 1000);
  }

I don't think this is the best solution because the timer occasionally skips a second to make uneven BackgroundTimer seems even.

ikurowski avatar Mar 14 '23 19:03 ikurowski

Maybe this can help you: https://aloukissas.medium.com/how-to-build-a-background-timer-in-expo-react-native-without-ejecting-ea7d67478408 You can update the eventDate in the function by substracting the difference between the start date and now. This is how I used it in my timer:

await AsyncStorage.setItem('start_time', new Date().toISOString());

BackgroundTimer.runBackgroundTimer(async () => {
      const startTime = await AsyncStorage.getItem('start_time');

      this.setState((prevState) => ({
        timeLeft:
          this.duration -
          differenceInSeconds(new Date(), Date.parse(startTime)),
      }));
    }, 1000);
  }

I don't think this is the best solution because the timer occasionally skips a second to make uneven BackgroundTimer seems even.

I am also experience this problem and I am trying to come up with a solution. Did you manage to fix it? @ikurowski

thatgriffith avatar May 11 '23 10:05 thatgriffith

For anyone still experiencing this issue, I solved it with the following:

  • For iOS it works with JS own setInterval so no additional package needed here.
  • For Android I found the package: https://github.com/juanamd/react-native-background-timer-android that states to be an improvement of this package. I gave it a go and it works perfect. However, in dev mode on real device when returning from background the UI could lag from time to time. But when testing on a production apk, it works smoothly. No delay in time or any other problem.

Hopefully this helps anyone still struggling with this problem.

thatgriffith avatar May 16 '23 09:05 thatgriffith