Sleep Stages Duration Calculator
Sleep Stages Duration Calculator π€
Β© SleepCalculators.Online
Sleep Efficiency: ${calculateEfficiency(values)}%
`;
}
function calculateEfficiency(values) {
const actualSleep = values.rem + values.light + values.deep;
const totalTime = actualSleep + values.wake;
return Math.round((actualSleep / totalTime) * 100);
}
function exportToExcel(values, total) {
const efficiency = calculateEfficiency(values);
const csvContent = `Sleep Stages Analysis\n\nStage,Duration (minutes)\nREM,${values.rem}\nLight,${values.light}\nDeep,${values.deep}\nWake,${values.wake}\n\nTotal Sleep Time,${total}\nSleep Efficiency,${efficiency}%`;
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'sleep_stages.csv';
a.click();
}
function copyToClipboard(values, total) {
const efficiency = calculateEfficiency(values);
const text = `Sleep Stages Analysis\n\nREM Sleep: ${values.rem} minutes\nLight Sleep: ${values.light} minutes\nDeep Sleep: ${values.deep} minutes\nWake Time: ${values.wake} minutes\n\nTotal Sleep Time: ${total} minutes\nSleep Efficiency: ${efficiency}%`;
navigator.clipboard.writeText(text)
.then(() => alert('Results copied to clipboard! π'))
.catch(err => console.error('Failed to copy text: ', err));
}
// Initialize progress bars
updateProgress();
// Add input event listeners
stages.forEach(stage => {
document.getElementById(`${stage}Input`).addEventListener('input', updateProgress);
});