How do I reset a localStorage
variable that is a boolean
that tracks whether a user has completed an action that day. When the time reaches, for example, 8 am local time
, it resets the boolean
to false
to show that the action hasn’t been completed yet.
If the user has completed the action that day, the localStorage
variable gets set to true
and we show the countdown to 8 am
.
Here are my methods that check for current time and time until 8 am
.
const twoDigits = (number) => {
return (number < 10 ? '0' : '') + number;
}
const countDown = () => {
var reset = new Date();
var now = new Date();
reset.setHours(8, 0, 0);
if(now > reset){
reset.setDate(reset.getDate() + 1);
}
var timeToReset = ((reset - now) / 1000);
var hours = ~~(timeToReset / 60 / 60) % 60;
var minutes = ~~(timeToReset / 60) % 60;
var seconds = ~~timeToReset % 60;
return `${twoDigits(hours)}:${twoDigits(minutes)}:${twoDigits(seconds)}`;
}
const intervalId = setInterval(() => {
console.log(countDown());
}, 1000);
// clear interval after 10 seconds so its not running indefinitely
setTimeout(() => {clearInterval(intervalId)}, 10000);