我最终得到的答案是停止尝试在后台运行计时器,而是使用时间戳计算两次之间的差值,因为它很难在跨平台的方式下可靠地在后台运行逻辑,而没有太多的时间增加了复杂性。
使用下面的函数,当计时器需要启动时,我将使用moment.js生成一个时间戳,然后在计时器需要停止时,生成另一个时间戳并计算它们之间的差异。
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 }`;
};
,
不是您问题的直接答案,而是可能使此问题不相关的建议...
假设您的用户实际上输入了锻炼的开始时间和结束时间,您将能够使用这些时间戳计算花费的锻炼时间。
从结束时间减去开始时间将为您提供持续的用户锻炼时间。
,
对于Android
,可以通过React-native
中的模块来解决。但这仅适用于Android
。
Headless JS
示例
import {HeadlessJsTaskError} from 'HeadlessJsTask';
module.exports = async (taskData) => {
const condition = ...;
if (!condition) {
throw new HeadlessJsTaskError();
}
};
本文链接:https://www.f2er.com/3158585.html