I want to write a program(for my degree) where a a counter will be lock when a countdown start and will unlock itself 15mins before the countdown stop
I am not sure of all the specifics of your program, but it should be possible. What you have here is a typical guard condition where you don’t let anything touch the countdown until the time now is less than 15 minutes from the set countdown stop time.
For example, assume your counter is set to stop at midnight on October 25th. Each time the code is executed, you look at the current time and calculate if the time is greater than or equal to October 24th 11:45pm. If it is, only then will you do something with the countdown. If it is less than 11:45pm on October 24th you will skip altering the countdown.
This can be down with a simple time calculation and an if statement. I hope you get the idea.
For the case of this example, I’ve set the target date to be 16 minutes from after the code is run. That way, after 1 minute of waiting, the lock should unlock.
const targetMinutes = new Date().getMinutes() + 16;
const targetDate = new Date().setMinutes(targetMinutes);
On the page we can have a locked and an unlocked padlock
<img id="locked" src="https://icon-library.com/images/698630-icon-114-lock-512.png">
<img id="unlocked" class="hide" src="https://icon-library.com/images/698632-icon-116-lock-open-512.png">
and css hides the unlocked padlock
.hide {
display: none;
}
Then we can use setInterval so that an update occurs every second:
setInterval(function () {
}, 1000);
and if the minutes are less than 15, we can unlock the padlock.
const timespan = countdown(targetDate);
if (timespan.minutes < 15) {
document.querySelector("#locked").classList.add("hide");
document.querySelector("#unlocked").classList.remove("hide");
}
Where does countdown come from? That’s a handy library to make things easier for us.
The timespan has a value property, that is negative when it’s behind a certain time/date, and positive when it’s afterwards and counting up. We want it to stop counting down when it reaches the time/date, so we can check for when the value is more than zero, then clear the interval.
const timer = setInterval(function () {
...
if (timespan.value > 0) {
clearInterval(timer);
}
}, 1000);
And I guess that you also want to see the minutes and seconds as it’s counting down, so we can add a countdown section to the HTML page:
<div id="countdown"></div>
and update that at the end of the interval:
const timer = setInterval(function () {
...
document.querySelector("#countdown").innerHTML = timespan.toString();
}, 1000);
Here is the full JS code:
const targetMinutes = new Date().getMinutes() + 1;
const targetDate = new Date().setMinutes(targetMinutes);
const timer = setInterval(function () {
const timespan = countdown(targetDate);
if (timespan.minutes < 15) {
document.querySelector("#locked").classList.add("hide");
document.querySelector("#unlocked").classList.remove("hide");
}
if (timespan.value > 0) {
clearInterval(timer);
}
document.querySelector("#countdown").innerHTML = timespan.toString();
}, 1000);
And, the full code is also up at https://jsfiddle.net/pmw57/mczu4vbe/1/ and hopefully helps with whatever degree you are working towards.
Out of interest, what degree are you working on?
Edit: And I just realized that I didn’t answer this in terms of nodejs. I missed that, sorry.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.