From 16d4dc758875bdef5fc12eef0f906903dcb64c1d Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 6 May 2023 11:37:55 +0100 Subject: [PATCH] stlap: slow down javascript when the user's not watching --- apps/stlap/app.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/stlap/app.js b/apps/stlap/app.js index 0c0bb33ca..ad5463180 100644 --- a/apps/stlap/app.js +++ b/apps/stlap/app.js @@ -157,7 +157,7 @@ function firstTimeStart(now, time) { elapsedTime: 0, }; lapFile = 'stlap-' + state.sessionStart + '.json'; - setupTimerInterval(); + setupTimerIntervalFast(); Bangle.buzz(200); drawButtons(); } @@ -201,13 +201,15 @@ function start(now, time) { state.elapsedTime += (state.pausedTime - state.startTime); state.startTime = now; state.running = true; - setupTimerInterval(); + setupTimerIntervalFast(); Bangle.buzz(200); drawTime(); drawButtons(); } Bangle.on("touch", (button, xy) => { + setupTimerIntervalFast(); + //In gesture mode, just turn on the light and then return if (gestureMode) { Bangle.setLCDPower(true); @@ -242,6 +244,8 @@ Bangle.on("touch", (button, xy) => { }); Bangle.on('swipe', direction => { + setupTimerIntervalFast(); + let now = (new Date()).getTime(); let time = getTime(); @@ -272,12 +276,23 @@ setWatch(() => { }, BTN1, { repeat: true }); let timerInterval; +let userWatching = false; + +function setupTimerIntervalFast() { + userWatching = true; + setupTimerInterval(); + + setTimeout(() => { + userWatching = false; + setupTimerInterval(); + }, 5000); +} function setupTimerInterval() { if (timerInterval !== undefined) { clearInterval(timerInterval); } - timerInterval = setInterval(drawTime, 10); + timerInterval = setInterval(drawTime, userWatching ? 10 : 1000); } function stopTimerInterval() { @@ -289,7 +304,7 @@ function stopTimerInterval() { drawTime(); if (state.running) { - setupTimerInterval(); + setupTimerIntervalFast(); } //Save our state when the app is closed @@ -300,5 +315,8 @@ E.on('kill', () => { } }); +// change interval depending of whether the user's looking +Bangle.on("twist", setupTimerIntervalFast); + Bangle.loadWidgets(); Bangle.drawWidgets(); \ No newline at end of file