Skip to content

Commit ea81104

Browse files
authored
Merge pull request #25 from MrAlders0n/copilot/fix-auto-ping-timer-issue
Fix auto-ping timer firing 8s early due to setInterval/countdown desync
2 parents ee11135 + 845db0c commit ea81104

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

content/wardrive.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,10 @@ async function sendPing(manual = false) {
553553

554554
// Set status to idle after map update
555555
if (state.connection) {
556-
// If in auto mode, start countdown. Otherwise, set to idle
556+
// If in auto mode, schedule next ping. Otherwise, set to idle
557557
if (state.running) {
558-
// Restart the countdown for next auto ping
559-
const intervalMs = getSelectedIntervalMs();
560-
startAutoCountdown(intervalMs);
558+
// Schedule the next auto ping with countdown
559+
scheduleNextAutoPing();
561560
} else {
562561
setStatus("Idle", "text-slate-300");
563562
}
@@ -596,7 +595,7 @@ function stopAutoPing(stopGps = false) {
596595
}
597596

598597
if (state.autoTimerId) {
599-
clearInterval(state.autoTimerId);
598+
clearTimeout(state.autoTimerId);
600599
state.autoTimerId = null;
601600
}
602601
stopAutoCountdown();
@@ -610,6 +609,22 @@ function stopAutoPing(stopGps = false) {
610609
updateAutoButton();
611610
releaseWakeLock();
612611
}
612+
function scheduleNextAutoPing() {
613+
if (!state.running) return;
614+
615+
const intervalMs = getSelectedIntervalMs();
616+
617+
// Start countdown immediately
618+
startAutoCountdown(intervalMs);
619+
620+
// Schedule the next ping
621+
state.autoTimerId = setTimeout(() => {
622+
if (state.running) {
623+
sendPing(false).catch(console.error);
624+
}
625+
}, intervalMs);
626+
}
627+
613628
function startAutoPing() {
614629
if (!state.connection) {
615630
alert("Connect to a MeshCore device first.");
@@ -626,7 +641,7 @@ function startAutoPing() {
626641

627642
// Clean up any existing auto-ping timer (but keep GPS watch running)
628643
if (state.autoTimerId) {
629-
clearInterval(state.autoTimerId);
644+
clearTimeout(state.autoTimerId);
630645
state.autoTimerId = null;
631646
}
632647
stopAutoCountdown();
@@ -640,14 +655,8 @@ function startAutoPing() {
640655
// Acquire wake lock for auto mode
641656
acquireWakeLock().catch(console.error);
642657

643-
// First ping immediately, then at selected interval
658+
// Send first ping immediately
644659
sendPing(false).catch(console.error);
645-
const intervalMs = getSelectedIntervalMs();
646-
state.autoTimerId = setInterval(() => {
647-
sendPing(false).catch(console.error);
648-
}, intervalMs);
649-
650-
// Countdown will be started after the first ping completes
651660
}
652661

653662
// ---- BLE connect / disconnect ----

0 commit comments

Comments
 (0)