05-14-2019 17:56 - edited 05-14-2019 18:04
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-14-2019 17:56 - edited 05-14-2019 18:04
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Answered! Go to the Best Answer.

- Labels:
-
JavaScript
Accepted Solutions
05-15-2019 01:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-15-2019 01:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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
}

05-15-2019 01:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-15-2019 01:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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
}

05-16-2019 10:43 - edited 05-16-2019 10:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-16-2019 10:43 - edited 05-16-2019 10:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

05-16-2019 10:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-16-2019 10:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
The device supports EcmaScript 5.1, so those are just standard features of JavaScript.
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

