// sleeptimer.js - Verwaltet die automatische Abschaltung und Restzeitanzeige window.SleepTimer = { timeoutId: null, endTime: null, onExpiry: null, init(onExpiryCallback) { this.onExpiry = onExpiryCallback; }, setTimer(minutes) { this.clear(); const durationMs = minutes * 60000; this.endTime = Date.now() + durationMs; this.timeoutId = setTimeout(() => { if (this.onExpiry) this.onExpiry(); this.clear(); }, durationMs); this.updateButtonUI(); }, clear() { if (this.timeoutId) { clearTimeout(this.timeoutId); this.timeoutId = null; } this.endTime = null; this.updateButtonUI(); }, updateButtonUI() { const btn = document.getElementById('open-sleep-timer-modal'); if (!btn) return; if (this.endTime) { const remaining = Math.max(0, Math.ceil((this.endTime - Date.now()) / 60000)); // Anzeige mit Icon und Restzeit btn.innerHTML = ` ${remaining} Min`; // Selbst-Aktualisierung alle 10 Sekunden (nur wenn Timer aktiv) if (this.timeoutId) { setTimeout(() => this.updateButtonUI(), 10000); } } else { // Standard-Text wenn kein Timer läuft btn.textContent = 'Sleeptimer'; } // Den "Timer aus"-Button im Modal zeigen/verstecken const offBtn = document.getElementById('off-sleep-timer-modal'); if (offBtn) { offBtn.style.display = this.timeoutId ? 'inline-block' : 'none'; } } };