Skip to content

Commit

Permalink
Ensure blocking mode timer is started even when set via a third-party…
Browse files Browse the repository at this point in the history
… API call

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Dec 27, 2023
1 parent c5c2c5b commit 0076e32
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions scripts/pi-hole/js/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ function secondsTimeSpanToHMS(s) {
return h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); //zero padding on minutes and seconds
}

function piholeChanged(blocking) {
var status = $("#status");
var ena = $("#pihole-enable");
var dis = $("#pihole-disable");
function piholeChanged(blocking, timer = null) {
const status = $("#status");
const ena = $("#pihole-enable");
const dis = $("#pihole-disable");
const enaT = $("#enableTimer");

if (timer !== null && parseFloat(timer) > 0) {
enaT.html(Date.now() + parseFloat(timer) * 1000);
setTimeout(countDown, 100);
}

switch (blocking) {
case "enabled": {
Expand Down Expand Up @@ -83,7 +89,7 @@ function piholeChanged(blocking) {
function countDown() {
var ena = $("#enableLabel");
var enaT = $("#enableTimer");
var target = new Date(parseInt(enaT.html(), 10));
var target = new Date(parseInt(enaT.text(), 10));
var seconds = Math.round((target.getTime() - Date.now()) / 1000);

//Stop and remove timer when user enabled early
Expand All @@ -97,7 +103,7 @@ function countDown() {
ena.text("Enable Blocking (" + secondsTimeSpanToHMS(seconds) + ")");
} else {
ena.text("Enable Blocking");
piholeChanged("enabled");
piholeChanged("enabled", null);
if (localStorage) {
localStorage.removeItem("countDownTarget");
}
Expand All @@ -116,7 +122,7 @@ function checkBlocking() {
method: "GET",
})
.done(function (data) {
piholeChanged(data.blocking);
piholeChanged(data.blocking, data.timer);
utils.setTimer(checkBlocking, REFRESH_INTERVAL.blocking);
})
.fail(function (data) {
Expand All @@ -126,8 +132,7 @@ function checkBlocking() {
}

function piholeChange(action, duration) {
var enaT = $("#enableTimer");
var btnStatus;
let btnStatus = null;

switch (action) {
case "enable":
Expand Down Expand Up @@ -155,11 +160,7 @@ function piholeChange(action, duration) {
.done(function (data) {
if (data.blocking === action + "d") {
btnStatus.html("");
piholeChanged(data.blocking);
if (duration > 0) {
enaT.html(Date.now() + duration * 1000);
setTimeout(countDown, 100);
}
piholeChanged(data.blocking, data.timer);
}
})
.fail(function (data) {
Expand Down

0 comments on commit 0076e32

Please sign in to comment.