Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopBackgroundTimer not working #310

Open
sankomil opened this issue Mar 11, 2021 · 6 comments
Open

stopBackgroundTimer not working #310

sankomil opened this issue Mar 11, 2021 · 6 comments

Comments

@sankomil
Copy link

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.

@usamaabutt
Copy link

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

@mifi
Copy link

mifi commented Nov 25, 2021

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

@gus-delpino
Copy link

Something that's working for me is:

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

Ghetto, but it works for me for iOS and Android

@sankomil
Copy link
Author

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

@kellensabhlok
Copy link

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

@alexander0205
Copy link

This work for me using promise.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants