I am currently building this timer on a webview where the timer will automatically updates with javascript and the timer will decrement every second.
The current problem is that when a user makes the phone go to sleep, the timer doesn't update accordingly and the countdown will be a bit off so I would want to update the timer through server side instead of relying on a function that will break if a user makes the phone go to sleep. Here is the current code that I have
<h2 id="timer">
1:00
<h2>
<script>
var countdown = new Date("<?php echo $row['end_timer'] ?>").getTime();
var now = new Date("<?php echo date('Y-m-d H:i:s') ?>")
var x = setInterval(function() {
// Increase the distance by 1
now.setSeconds(now.getSeconds() +
var distance = countdown - (now.getTime());
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("timer").innerHTML = (seconds < 10 ? '0' : '') + seconds;
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "00";
location.reload();
}
}, 1000);
</script>