1

Basically, I am trying to emulate something just like https://www.online-stopwatch.com/countdown-timer/ for use in a web app. Works fine using settimeout, but when the screen is turned off on Android, for example, javascript stops running (or is severely delayed) so a 5 minute timer might take 15 minutes to ring, or 5 minutes to ring.

Any solution would be much appreciated. I've also tried support workers but those also get stopped when the screen is off.

I can't figure out what technology they are using on the aforementioned website, but it looks like animation or something, and from my tests IS accurate even when the screen is off.

Thank you!

Edit: Nope - I did a number of more tests and even their timer does not work reliably on android.

cirrus123
  • 49
  • 3
  • Please visit [help], take [tour] to see what and [ask]. Do some research, [search for related topics on SO](https://www.google.com/search?q=javascript+countdown+that+works+when+screen+is+off+site:stackoverflow.com); if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jan 12 '23 at 06:30
  • Android takes it upon itself to select certain apps for 'Deep Sleep', preventing them from running outside of very intermittant periods like you described. Is the app being optimised/sleeping? See https://www.androidpolice.com/prevent-apps-from-sleeping-in-the-background-on-android/ – 1owk3y Jan 12 '23 at 06:33
  • Anyway, the script uses the date object: https://www.online-stopwatch.com/js/uptimer1.js – mplungjan Jan 12 '23 at 06:35
  • Hi there - yes - the app does go to sleep, but we cannot expect all users to change their app settings to use the web app unfortunately. As for the script on online-stopwatch - it must use something other than settimeout as settimeout does go to sleep when the screen is closed – cirrus123 Jan 12 '23 at 12:11

1 Answers1

1

Edit: the question was actually about a website being able to trigger some kind of sound, alert or notification while the phone screen is off, not about keeping track of the time. That is not possible due to permissions (thankfully).

Apps have access to additional permissions that might make this feasible to do as an app.


Original answer basically answering "How to measure elapsed time in a browser on mobile from clicking a button even when screen is off or browser is closed":

What I would do is as soon as the timer is started you store the timer start time in localstorage and then use a setInterval to update the UI based on your stored value. I would make this accurate to the second to avoid a very heavy UI update cycle. Something like this works (tested on Android / Opera)

(Cannot use a snippet due to localStorage allow-same-origin)

<div>-</div>
<button>Start</button>

<script>
const out = document.querySelector("div")
const btn = document.querySelector("button")

let timer, startTime;

btn.addEventListener("click", () => {
  // If timer is started, reset
  if (timer) {
    clearInterval(timer)
  }
  startTime = Date.now()
  localStorage.setItem("startTime", startTime)

  timer = setInterval(() => {
    const now = Date.now()
    const millis = now - Number(localStorage.startTime)
    const seconds = Math.floor((millis / 1000) % 60)
    const minutes = Math.floor((millis / 1000 / 60) % 60)
    out.innerText = minutes + ":" + seconds
  }, 1000)

  console.log("Started interval timer", timer)
})
</script>

You could make it even work between browser shutdowns by checking the localstorage during page load

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • Sorry, is this still reliant on settimeout to run reliably even if the screen is off? Because from my tests and what I've read, settimeout cannot be relied upon, which is why the question has been posted – cirrus123 Jan 12 '23 at 12:21
  • Nope, it doesn't rely on that. The point of this is to store the starting time in localStorage and the setInterval is just there to update the user interface @cirrus123 – Joel Peltonen Jan 13 '23 at 13:19
  • If you want persistence when the browser is closed, this can be easily extended to include that as well. The whole key to your problem is really localStorage. @cirrus123 – Joel Peltonen Jan 13 '23 at 13:20
  • The problem is not keeping track of time - it is so that at the end of a certain time, an alarm is triggered. If the browser is asleep due to the screen being closed, even with using local storage it does not seem to trigger? – cirrus123 Jan 23 '23 at 06:09
  • @cirrus123 OH right, now I finally understand what you are asking. As far as I know that's not possible in any way without creating an actual app that has access to push notifications and additional permissions on the system. Thankfully - I would _hate_ to see websites being able to trigger anything while my phone is asleep – Joel Peltonen Jan 24 '23 at 16:37