// sleeptimer.js - Verwaltet die automatische Abschaltung window.SleepTimer = { timeoutId: null, endTime: null, onExpiry: null, // Callback-Funktion, die bei Ablauf gerufen wird init(onExpiryCallback) { this.onExpiry = onExpiryCallback; }, start(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)); btn.innerHTML = ` ${remaining} Min`; // Alle 10 Sekunden die Anzeige aktualisieren setTimeout(() => this.updateButtonUI(), 10000); } else { btn.textContent = 'Sleep Timer'; } // Den "Aus"-Button im Modal steuern const offBtn = document.getElementById('off-sleep-timer-modal'); if (offBtn) { offBtn.style.display = this.timeoutId ? 'inline-block' : 'none'; } }, isActive() { return this.timeoutId !== null; } };