Radioplayer-with-Presets/sleeptimer.js
simonpipe 6c1b185fc6 Upload files to "/"
Neu aufgeteilter Code
2025-12-18 17:17:16 +01:00

57 lines
No EOL
1.6 KiB
JavaScript

// 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 = `<i class="fas fa-hourglass-half"></i> ${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;
}
};