Skip to content

Commit 6c72a83

Browse files
authored
Merge pull request #26 from ssfinney/codex/fix-timer-reset-on-game-resume
Fix timer reset on resume by restoring normalized elapsed time
2 parents 3c52847 + 1d3bcee commit 6c72a83

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

script.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,19 @@
182182
(Math.floor(selected.r/3)===Math.floor(r/3) && Math.floor(selected.c/3)===Math.floor(c/3));
183183
}
184184

185+
function getPersistedElapsedTime(data){
186+
return data?.elapsed ?? data?.timeElapsed ?? data?.timer ?? data?.timeSeconds;
187+
}
188+
189+
function normalizeElapsed(value){
190+
const parsed=Number(value);
191+
if(!Number.isFinite(parsed) || parsed<0) return 0;
192+
return Math.floor(parsed);
193+
}
194+
185195
function formatTime(t){
186-
return `${String(Math.floor(t/60)).padStart(2,'0')}:${String(t%60).padStart(2,'0')}`;
196+
const normalized=normalizeElapsed(t);
197+
return `${String(Math.floor(normalized/60)).padStart(2,'0')}:${String(normalized%60).padStart(2,'0')}`;
187198
}
188199

189200
// ── Notes ────────────────────────────────────────────────────────────────
@@ -566,7 +577,7 @@
566577
startingGrid=data.startingGrid||data.grid.map(row=>row.slice());
567578
notes=data.notes.map(row=>row.map(arr=>new Set(arr)));
568579
selected=data.selected;
569-
elapsed=data.elapsed||0;
580+
elapsed=normalizeElapsed(getPersistedElapsedTime(data));
570581
notesMode=!!data.notesMode;
571582
autoCleanup=data.autoCleanup!==false;
572583
difficultyEl.value=data.difficulty||'medium';
@@ -946,7 +957,7 @@
946957
try{
947958
const saved=JSON.parse(raw);
948959
const modal=document.getElementById('resumeModal');
949-
timeStat.textContent=formatTime(Number(saved.elapsed) || 0);
960+
timeStat.textContent=formatTime(getPersistedElapsedTime(saved));
950961
modal.hidden=false;
951962
document.getElementById('resumeYesBtn').onclick=()=>{ modal.hidden=true; applyLoadedData(saved); };
952963
document.getElementById('resumeNoBtn').onclick=()=>{ modal.hidden=true; newGame(); };

0 commit comments

Comments
 (0)