Created
November 13, 2015 14:05
-
-
Save brianloveswords/20994820084a1968205d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<audio id="alarm" src="alarm.ogg" controls></audio> | |
<script> | |
const alarmElement = document.getElementById('alarm'); | |
const alarmTimes = [ | |
new Date('2015-11-13 07:00 -0500'), | |
new Date('2015-11-13 08:00 -0500'), | |
new Date('2015-11-13 08:30 -0500'), | |
]; | |
function playAlarm() { | |
alarmElement.currentTime = 0; | |
alarmElement.play(); | |
} | |
function checkTime(time) { | |
if (Date.now() >= time) { | |
return playAlarm(); | |
} | |
const diff = time - Date.now(); | |
console.log('%s seconds until alarm', diff/1000|0); | |
setTimeout(playAlarm, diff); | |
} | |
function stopAlarm() { | |
alarmElement.pause(); | |
} | |
alarmTimes.forEach(time => checkTime(time)); | |
document.body.addEventListener('keypress', e => { | |
// Spacebar | |
if (e.keyCode == 32) { | |
stopAlarm() | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I learnt two little tricks :D
Thanks 👍