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

Clock updates every minute but API data does not

I have a watch face that has a clock and some API data pulled from the web. I see that the following refreshes the clock every minute:

// Update the clock every minute
clock.granularity = "minutes";

How would I set the fetched API data to update every minute as well? Should it be something like apiData.granularity = "minutes"; ?

Here is my app/index.js file:

import document from "document";
import * as messaging from "messaging";
import clock from "clock";
import { preferences } from "user-settings";
import * as util from "../common/utils";

// Update the clock every minute
clock.granularity = "minutes";

let demotext = document.getElementById("demotext");

// Listen for the onopen event
messaging.peerSocket.onopen = function() {
  console.log('loading');
  messaging.peerSocket.send("Hi!");
}

// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
  console.log('event', JSON.stringify(evt.data))
  var dataTest = JSON.stringify(evt.data);
  this.ui = document.getElementById("demotext")
  this.ui.text = dataTest;
}

//Listen fo rthe onerror event
messaging.peerSocket.onerror = function(err) {
  
}

// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");
const apiData = document.getElementById("apiData")

// Update the <text> element every tick with the current time
clock.ontick = (evnt) => {
  let today = evnt.date;
  let hours = today.getHours();
  if (preferences.clockDisplay === "12h") {
    // 12h format
    hours = hours % 12 || 12;
  } else {
    // 24h format
    hours = util.zeroPad(hours);
  }
  let mins = util.zeroPad(today.getMinutes());
  myLabel.text = `${hours}:${mins}`;
}

 

Best Answer
0 Votes
2 REPLIES 2

At a quick glance, I think your code will update the watch screen whenever a message is received. What you need to do is to ensure that messages are received as often as you want them. You'll have to do that in the companion.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Just to add to this... either use the companion wakeInterval to fetch new data, or when the screen wakes tell the companion to fetch data by sending it a message. Ideally, you only want to minimize impact on the battery by not requesting when the display is off.

Best Answer
0 Votes