react-native-background-timer
Advanced tools
Comparing version
74
index.js
'use strict'; | ||
import { NativeModules } from 'react-native'; | ||
module.exports = NativeModules.BackgroundTimer; | ||
import { | ||
NativeModules, | ||
DeviceEventEmitter | ||
} from 'react-native'; | ||
const RNBackgroundTimer = NativeModules.BackgroundTimer || NativeModules.RNBackgroundTimer; | ||
let uniqueId = 0; | ||
const callbacks = {}; | ||
const BackgroundTimer = { | ||
// Original API | ||
start(delay) { | ||
return RNBackgroundTimer.start(delay); | ||
}, | ||
stop() { | ||
return RNBackgroundTimer.stop(); | ||
}, | ||
// New API, allowing for multiple timers | ||
setTimeout(callback, timeout) { | ||
const timeoutId = ++uniqueId; | ||
callbacks[timeoutId] = { | ||
callback: callback, | ||
interval: false, | ||
timeout: timeout | ||
}; | ||
RNBackgroundTimer.setTimeout(timeoutId, timeout); | ||
return timeoutId; | ||
}, | ||
clearTimeout(timeoutId) { | ||
if (callbacks[timeoutId]) { | ||
delete callbacks[timeoutId]; | ||
//RNBackgroundTimer.clearTimeout(timeoutId); | ||
} | ||
}, | ||
setInterval(callback, timeout) { | ||
const intervalId = ++uniqueId; | ||
callbacks[intervalId] = { | ||
callback: callback, | ||
interval: true, | ||
timeout: timeout | ||
}; | ||
RNBackgroundTimer.setTimeout(intervalId, timeout); | ||
return intervalId; | ||
}, | ||
clearInterval(intervalId) { | ||
if (callbacks[intervalId]) { | ||
delete callbacks[intervalId]; | ||
//RNBackgroundTimer.clearTimeout(intervalId); | ||
} | ||
} | ||
}; | ||
DeviceEventEmitter.addListener('backgroundTimer.timeout', (id) => { | ||
if (callbacks[id]) { | ||
const callback = callbacks[id].callback; | ||
if (!callbacks[id].interval) { | ||
delete callbacks[id]; | ||
} | ||
else { | ||
RNBackgroundTimer.setTimeout(id, callbacks[id].timeout); | ||
} | ||
callback(); | ||
} | ||
}); | ||
module.exports = BackgroundTimer; |
{ | ||
"name": "react-native-background-timer", | ||
"version": "0.1.2", | ||
"description": "Emit event periodically (also when application is running in the background)", | ||
"version": "0.1.3", | ||
"description": "Emit event periodically (even when app is in the background)", | ||
"keywords": [ | ||
"android", | ||
"ios", | ||
"react-component", | ||
@@ -8,0 +9,0 @@ "react-native", |
# React Native Background Timer | ||
Emit event periodically (also when application is running in the background). | ||
Emit event periodically (even when app is in the background). | ||
Currently for Android only. | ||
## Installation | ||
- `npm i react-native-background-timer --save` | ||
- `react-native link` | ||
## Instalation | ||
- `npm install react-native-background-timer --save` | ||
- `rnpm 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. | ||
## Usage | ||
```javascript | ||
import BackgroundTimer from 'react-native-background-timer'; | ||
``` | ||
```javascript | ||
// 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); | ||
``` | ||
```javascript | ||
// Start a timer that runs once after X milliseconds | ||
const timeoutId = BackgroundTimer.setTimeout(() => { | ||
// this will be executed once after 10 seconds | ||
// even when app is the the background | ||
console.log('tac'); | ||
}, 10000); | ||
// Cancel the timeout if necessary | ||
BackgroundTimer.clearTimeout(timeoutId); | ||
``` | ||
### Obsolete | ||
Obsolete usage which doesn't allows to use multiple background timers. | ||
```js | ||
var {DeviceEventEmitter} = React; | ||
var BackgroundTimer = require('react-native-background-timer'); | ||
import { | ||
DeviceEventEmitter, | ||
NativeAppEventEmitter, | ||
Platform, | ||
} from 'react-native'; | ||
import BackgroundTimer from 'react-native-background-timer'; | ||
``` | ||
```js | ||
// start timing | ||
const EventEmitter = Platform.select({ | ||
ios: () => NativeAppEventEmitter, | ||
android: () => DeviceEventEmitter, | ||
})(); | ||
``` | ||
```js | ||
// start a global timer | ||
BackgroundTimer.start(5000); // delay in milliseconds | ||
@@ -23,9 +69,9 @@ ``` | ||
// this will be executed every 5 seconds | ||
// also when application is running in the background | ||
console.log('tic'); | ||
// even when app is the the background | ||
console.log('toe'); | ||
}); | ||
``` | ||
```js | ||
// you can explicitly stop timing | ||
// stop the timer | ||
BackgroundTimer.stop(); | ||
``` |
Sorry, the diff of this file is not supported yet
19852
269.13%12
33.33%152
108.22%77
148.39%