Skip to content

Emit event periodically (also when application is running in the background)

License

Notifications You must be signed in to change notification settings

IjzerenHein/react-native-background-timer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Native Background Timer

Emit event periodically (also when application is running in the background).

Installation

  • npm install react-native-background-timer --save
  • react-native link

Usage

var {DeviceEventEmitter} = React;
var BackgroundTimer = require('react-native-background-timer');
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds
// listen for event
DeviceEventEmitter.addListener('backgroundTimer', () => {
	// this will be executed every 5 seconds
	// also when application is running in the background
	console.log('tic');
});
// stop the timer
BackgroundTimer.stop();

setInterval, setTimeout

Alternatively, 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.

var BackgroundTimer = require('react-native-background-timer');
// Start a timer that runs continuous after X msec
const intervalId = BackgroundTimer.setInterval(() => {
	// this will be executed every 200 ms
	// even when app is in the background
	console.log('tac');
}, 200);

// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// Start a timer that runs once after X msec
const timeoutId = BackgroundTimer.setTimeout(() => {
	// this will be executed once after 10 seconds
	// even when app goed into the background
  	console.log('toe');
}, 10000);

// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);

About

Emit event periodically (also when application is running in the background)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 41.9%
  • Objective-C 38.5%
  • JavaScript 19.6%