diff --git a/index.html b/index.html
index 23c31b4..372531e 100644
--- a/index.html
+++ b/index.html
@@ -9,7 +9,7 @@
-
+
diff --git a/js/app.js b/js/app.js
index 3c10656..e5b1ca3 100644
--- a/js/app.js
+++ b/js/app.js
@@ -30,9 +30,11 @@ try {
// Corrupt storage — start fresh
}
-// Call after any state mutation to persist automatically
+// Persist immediately. Used by the debounced saveState and the flush handlers.
let storageWarned = false;
-export function saveState() {
+let saveTimer = null;
+function writeState() {
+ if (saveTimer !== null) { clearTimeout(saveTimer); saveTimer = null; }
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify({
employees: state.employees,
@@ -52,6 +54,19 @@ export function saveState() {
}
}
+// Call after any state mutation. Debounced to coalesce rapid changes; a pending
+// write is flushed on page hide/unload so the last change is never lost.
+export function saveState() {
+ if (saveTimer !== null) clearTimeout(saveTimer);
+ saveTimer = setTimeout(writeState, 400);
+}
+
+const flushPending = () => { if (saveTimer !== null) writeState(); };
+window.addEventListener('beforeunload', flushPending);
+document.addEventListener('visibilitychange', () => {
+ if (document.visibilityState === 'hidden') flushPending();
+});
+
// Tab switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {