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

Toggle buttons don't hold value

ANSWERED

Hi,

 

I'm new to Fitbit OS programming. To get familiar with the code, I'm trying to create a watch face which displays the time in a binary way. Not usefull, I know, but it's just for testing. Showing the time in binary way is OK, but now I added 2 buttons to toggle between binary and decimal display. The code below is not complete yet, but the problem is that every second the toggle swithes back to Binary. If I click the Decimal button, the values in the console logs change, so they work :). How do I prevent this behaviour, so that the buttons don't toggle back every update of the screen?

 

thx, Ben


scherm.PNG 

// Update the clock every second
clock.granularity = "seconds";

// Get a handle on the <text> elements
const labelHoursMinutes = document.getElementById("labelHoursMinutes");
const labelSeconds = document.getElementById("labelSeconds");
const buttonBinary = document.getElementById("buttonBinary");
const buttonDecimal = document.getElementById("buttonDecimal");

// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
  let today = evt.date;
  let hours = today.getHours();
  let mins = today.getMinutes();
  let seconds = today.getSeconds();
  console.log("ButtonBinary: " + buttonBinary.value);
  console.log("ButtonDecimal: " + buttonDecimal.value);
  if (buttonBinary.value = 1) {
    // Convert to bin
    hours = util.zeroPad(util.toBin(hours));
    mins = util.zeroPad(util.toBin(mins));
    seconds = util.zeroPad(util.toBin(seconds));
  }
  labelHoursMinutes.text = `${hours}:${mins}`;
  labelSeconds.text = `${seconds}`;  
}  

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

The problem is this line:

  if (buttonBinary.value = 1) {

You're actually setting .value to 1, not checking its value.

You want

  if (buttonBinary.value === 1) {

 

 

View best answer in original post

Best Answer
2 REPLIES 2

The problem is this line:

  if (buttonBinary.value = 1) {

You're actually setting .value to 1, not checking its value.

You want

  if (buttonBinary.value === 1) {

 

 

Best Answer

Thx! As I said, new to Fitbit Dev / Java script 🙂 🙂

Best Answer
0 Votes