score:5

Accepted answer

The answer I ended up with was to stop trying to run a timer in the background and instead use timestamps to compute the difference between 2 times since its difficult to reliably run logic in the background in a cross platform way without a lot of added complexity.

Using the function below I would generate a timestamp with moment.js when the timer needs to start and then when the timer needs to stop generate another timestamp and calculate the difference between them.

const getCurrentTimeInWithMomentLibary = moment().format( 'MM/DD/YYYY HH:mm:ss' );


const diffTime = ( then, now ) => {
  const ms = moment( now, 'MM/DD/YYYY HH:mm:ss' )
    .diff( moment( then, 'MM/DD/YYYY HH:mm:ss' ) );
  const duration = moment.duration( ms );
  const hours = Math.floor( duration.asHours() );

  const currentDate = moment().format( 'MM/DD/YYYY' );
  const time = `${ hours }:${ moment.utc( ms ).format( 'mm:ss' ) }`;
  return `${ currentDate } ${ time }`;
};

score:0

For Android, it can be resolved through a module in React-native. But it only works on Android.

Headless JS

Example

import {HeadlessJsTaskError} from 'HeadlessJsTask';

module.exports = async (taskData) => {
  const condition = ...;
  if (!condition) {
    throw new HeadlessJsTaskError();
  }
};

score:1

Not a direct answer to your question but a suggestion that might make this issue irrelevant...

Assuming your users actually enter a start and end time for their workout, you will be able to calculate the amount of time spent working out using these timestamps.

Subtracting the start time from the end time would give you a duration of the users workout.


Related Query

More Query from same tag