React Native Background Timer
Emit event periodically (even when app is in the background).
Installation
npm i react-native-background-timer --save
react-native link
Usage
You can use the setInterval
and setTimeout
functions.
This API is identical to that of react-native
and can be used to quickly replace existing timers
with background timers.
import BackgroundTimer from 'react-native-background-timer';
const intervalId = BackgroundTimer.setInterval(() => {
console.log('tic');
}, 200);
BackgroundTimer.clearInterval(intervalId);
const timeoutId = BackgroundTimer.setTimeout(() => {
console.log('tac');
}, 10000);
BackgroundTimer.clearTimeout(timeoutId);
Obsolete
Obsolete usage which doesn't allows to use multiple background timers.
import {
DeviceEventEmitter,
NativeAppEventEmitter,
Platform,
} from 'react-native';
import BackgroundTimer from 'react-native-background-timer';
const EventEmitter = Platform.select({
ios: () => NativeAppEventEmitter,
android: () => DeviceEventEmitter,
})();
BackgroundTimer.start(5000);
EventEmitter.addListener('backgroundTimer', () => {
console.log('toe');
});
BackgroundTimer.stop();