Giter Site home page Giter Site logo

julienkode / react-native-background-timer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ocetnik/react-native-background-timer

1.0 3.0 2.0 21 KB

Emit event periodically (even when app is in the background)

License: MIT License

Java 38.71% JavaScript 18.05% Objective-C 35.54% Ruby 7.71%

react-native-background-timer's Introduction

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

Installation using CocoaPods on iOS

  • npm i react-native-background-timer --save
  • add the following to your Podfile: pod 'react-native-background-timer', :path => '../node_modules/react-native-background-timer'

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';
// 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);
// 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.

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

import BackgroundTimer from 'react-native-background-timer';
const EventEmitter = Platform.select({
  ios: () => NativeAppEventEmitter,
  android: () => DeviceEventEmitter,
})();
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds
// listen for event
EventEmitter.addListener('backgroundTimer', () => {
	// this will be executed every 5 seconds
	// even when app is the the background
	console.log('toe');
});
// stop the timer
BackgroundTimer.stop();

react-native-background-timer's People

Contributors

ijzerenhein avatar julienkode avatar ocetnik avatar saghul avatar st0ffern avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

arhs theasc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.