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.stopBackgroundTimer(); not working

See original GitHub issue

Hello everyone. I am using react-native-background-timer. And I wanted to perform a function for once only. But the cross platform function BackgroundTimer.runBackgroundTimer() keeps executing the function after every interval. I tried stopping it from the function but it keeps executing the code. I dont know what I am doing wrong. I dont want to use other functions as they are platform dependent. I wanted to use this function because it is cross platform.

Note: I wanted to run the function for once only. My code;

`componentDidMount(){ BackgroundTimer.runBackgroundTimer(() => { this.deleteAll() }, 18000); }

deleteAll=()=>{ console.log(‘Deleted’) BackgroundTimer.stopBackgroundTimer();

}` Any other cross platform approach for executing the function ‘once’ only will be appreciated. Thanks

Will be waiting for reply.

RN Version: 0.63.2 BG-Timer version: Latest package.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
scrapecodercommented, Sep 21, 2020

@aashiriftikhar

 import { DeviceEventEmitter, 
 NativeAppEventEmitter, Platform } from 'react- 
  native';
 import BackgroundTimer from 'react-native-background-timer';

const EventEmitter = Platform.select({
ios: () => NativeAppEventEmitter
android: () => DeviceEventEmitter,
 })();

  class BackgroundTimer {
   static setTimeout(callback, delay) {
    BackgroundTimer.start();
    this.backgroundListener = 
     EventEmitter.addListener("backgroundTimer", () => {
       this.backgroundTimer = 
      BackgroundTimer.setTimeout(callback, delay);
    });
    return this.backgroundListener;
}

static clearTimeout(timer) {
    if (timer) timer.remove();
    if (this.backgroundTimer) BackgroundTimer.clearTimeout(this.backgroundTimer);
    BackgroundTimer.stop();
  }
}

  export default BackgroundTimer

Try with this:

   const backgroundTimeout = BackgroundTimer.setTimeout(callback, 1000);
   BackgroundTimer.clearTimeout(timer)
1reaction
koushikv04commented, Sep 16, 2020

same here

Read more comments on GitHub >

github_iconTop Results From Across the Web

stopBackgroundTimer not working · Issue #310 - GitHub
Something that's working for me is: setTimeout(() => BackgroundTimer.stopBackgroundTimer(), 0);. Ghetto, but it works for me for iOS and Android ...
Read more >
React Native background timer never stops - Stack Overflow
the timer is stopping when the app is in the background. While with this piece of code: const intervalId = BackgroundTimer.setInterval(() => { ......
Read more >
How to use the react-native-background-timer.stop function in ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
react-native-background-timer | Yarn - Package Manager
React Native Background Timer. Emit event periodically (even when app is in the background). Installation. If you use Expo to create a project...
Read more >
[Solved]-React Native Background Timer with QR-code-React ...
BackgroundTimer.stopBackgroundTimer(); //to stop previous background timer BackgroundTimer.runBackgroundTimer(() => { //write code here that will be called ...
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