03-06-2019 16:39
03-06-2019 16:39
Hello,
I'm trying to build a timer app that displays a count down timer like the in-built one. However I am quite stuck in making it display the deci-seconds (10th of seconds ). Is there a way to achieve this goal?
Thanks!
Answered! Go to the Best Answer.
03-12-2019 15:58 - edited 03-12-2019 16:12
03-12-2019 15:58 - edited 03-12-2019 16:12
I'm not sure you'll get that level of granularity, but you can try something like:
1. use the display api to detect when the screen comes on
2. start a timer using setInterval()
3. update the display to show the time
import { display } from "display";
let intervalID; display.addEventListener("change", () => { if (display.on) { start(); } else { stop(); } }) function start() { // display goes on
intervalID = setInterval(updateDisplay, 100); // 10ths second } function stop() { // display goes off
if(intervalID) { clearInterval(intervalID); } }
function updateDisplay() {
let now = new Date();
let ms = now.getMilliseconds();
// set appropriate image href
}
03-12-2019 15:58 - edited 03-12-2019 16:12
03-12-2019 15:58 - edited 03-12-2019 16:12
I'm not sure you'll get that level of granularity, but you can try something like:
1. use the display api to detect when the screen comes on
2. start a timer using setInterval()
3. update the display to show the time
import { display } from "display";
let intervalID; display.addEventListener("change", () => { if (display.on) { start(); } else { stop(); } }) function start() { // display goes on
intervalID = setInterval(updateDisplay, 100); // 10ths second } function stop() { // display goes off
if(intervalID) { clearInterval(intervalID); } }
function updateDisplay() {
let now = new Date();
let ms = now.getMilliseconds();
// set appropriate image href
}