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

stopBackgroundTimer not working

Open sankomil opened this issue 4 years ago • 6 comments

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.

sankomil avatar Mar 11 '21 07:03 sankomil

I am also having the exact same issue any solution about this ?

usamaabutt avatar Jun 02 '21 11:06 usamaabutt

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

mifi avatar Nov 25 '21 13:11 mifi

Something that's working for me is:

setTimeout(() => BackgroundTimer.stopBackgroundTimer(), 0);

Ghetto, but it works for me for iOS and Android

gus-delpino avatar Jun 29 '22 07:06 gus-delpino

Ah great, I'll give it a try, thanks!

sankomil avatar Jun 29 '22 07:06 sankomil

@gus-delpino thank you this solved my problem too!

kellensabhlok avatar Sep 09 '22 17:09 kellensabhlok

This work for me using promise.

const PromiseValue = new Promise((resolve, reject) => {
    BackgroundTimer.runBackgroundTimer(() => {
      resolve();
        reject()
    }, 3000);
  });
  PromiseValue.finally(() =>
    BackgroundTimer.stopBackgroundTimer(),
  );

alexander0205 avatar Jan 15 '23 11:01 alexander0205