stopBackgroundTimer not working
Hi, I was creating a simple timer screen for an app and was using the library to update the timer every minute as so:
const [timer,setTimer]=useState(moment(new Date()).format('hh:mm:ss A (DD/MM/YYYY)');
BackgroundTimer.runBackgroundTimer(() => {
//code that will be called every 3 seconds
setTimer(moment(new Date()).format('hh:mm:ss A (DD/MM/YYYY)');
},
60000);
And have a button element, which when pressed should stop the timer, like so:
<Button onPress={()=>{BackgroundTimer.stopBackgroundTimer(); }}>Stop</Button>
However despite pressing the button, the timer does not stop and continues to update the app.
I am also having the exact same issue any solution about this ?
For me the setTimeout function works (as described in readme):
// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
// this will be executed every 200 ms
// even when app is the the background
console.log('tic');
}, 200);
// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
Something that's working for me is:
setTimeout(() => BackgroundTimer.stopBackgroundTimer(), 0);
Ghetto, but it works for me for iOS and Android
Ah great, I'll give it a try, thanks!
@gus-delpino thank you this solved my problem too!
This work for me using promise.
const PromiseValue = new Promise((resolve, reject) => {
BackgroundTimer.runBackgroundTimer(() => {
resolve();
reject()
}, 3000);
});
PromiseValue.finally(() =>
BackgroundTimer.stopBackgroundTimer(),
);