question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BackgroundTimer.setInterval is not synchronized with the actual timer

See original GitHub issue

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);

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

3reactions
jnjacobsoncommented, Dec 22, 2020

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);
  }
1reaction
maxim-greencommented, Apr 12, 2022

@jnjacobson thanks! That really helped!

Read more comments on GitHub >

github_iconTop Results From Across the Web

the time is not correct · Issue #250 · ocetnik/react-native ...
Same issue I'm facing. Background.setInterval is not synchronized with the actual time. it running 4-5 seconds more than the actual time ...
Read more >
react-native-background-timer - Bountysource
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...
Read more >
Make JavaScript interval synchronize with actual time
The basic idea is not to use setInterval() , but rather to calculate each time how many milliseconds later you want the loop...
Read more >
722410 - setInterval stops when window goes out of focus
We have decent coverage for setTimeout, but not setInterval. - --disable-background-timer-throttling seems broken at the moment.
Read more >
Why Javascript timer is unreliable, and how can you fix it
Remember when I said Javascript is single threaded, well… that's true, but not strictly. Say when you run a block of code inside...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found