Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Stopping an exercise after X steps.

ANSWERED

Is there any way to stop an exercise after a certain amount of steps? I'm trying to make a timed challenge where the user needs to make X amount of steps within an hour. I've tried several things but everything results in my Fitbit or app crashing.

 

The gist of my what I'm trying to do is here:

 

btn3.onactivate = function(evt){
exercise.start("Basic exercise");
challenge();
}

function challenge(){
//Track steps until goal is reached, or until an hour has passed.
}

Any help would be appreciated!

 

Edit: I have tried a while loop which crashed the app and a recursive function which crashed the Fitbit all together.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You'll need to get the start time, and current number of steps when the exercise starts, then periodically check the difference between the time, and the step count.

 

Something like this:

 

import { today } from "user-activity";

let startTime;
let startSteps;
let timerId;

btn3.onactivate = (evt) => { startTime = new Date(); startSteps = today.adjusted.steps;
timerId = setInterval(checkStatus, 60 * 1000); // start exercise }

function checkStatus() {
// check now vs. startTime - is it within an hour
// check startSteps vs. today.adjusted.steps
// if meets criteria, clearInterval(timerId) and stop exercise
}

 

 

View best answer in original post

Best Answer
0 Votes
3 REPLIES 3

You'll need to get the start time, and current number of steps when the exercise starts, then periodically check the difference between the time, and the step count.

 

Something like this:

 

import { today } from "user-activity";

let startTime;
let startSteps;
let timerId;

btn3.onactivate = (evt) => { startTime = new Date(); startSteps = today.adjusted.steps;
timerId = setInterval(checkStatus, 60 * 1000); // start exercise }

function checkStatus() {
// check now vs. startTime - is it within an hour
// check startSteps vs. today.adjusted.steps
// if meets criteria, clearInterval(timerId) and stop exercise
}

 

 

Best Answer
0 Votes

Would you be able to provide API links for the setInterval and Date methods? I'm new to JavaScript and cling to the API for assistance.

 

Edit/Note: I'm asking because I can't seem to find them on my phone and I won't have access to my desktop for a few days and I'd rather learn it inside out before sitting down to code again.

Best Answer
0 Votes
Best Answer
0 Votes