Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Created May 20, 2012 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larzconwell/2743463 to your computer and use it in GitHub Desktop.
Save larzconwell/2743463 to your computer and use it in GitHub Desktop.
alarmTime = "13:30"; // 1:30 pm
set_alarm = function() {
// Convert alarmTime to 24 format if it's 12 format
var alarmCopy = (Options.timeFormat === '12') ? toggle_time_format(alarmTime) : alarmTime
, time = new Date()
, cHour = time.getHours()
, cMin = time.getMinutes()
// Extract hour and minute from alarmCopy string
, aHour = Number(alarmCopy.replace(/:[0-9]+/, ''))
, aMin = Number(alarmCopy.replace(/[0-9]+:/, ''))
, millHour
, millMin
, tillAlarm;
// This kind of works, but messes up horribly
// - when the alarmTime is before the current time
if(aHour < cHour) {
millHour = ((24 - cHour) + aHour) * 3600000;
} else millHour = (aHour - cHour) * 3600000;
if(aMin < cMin) {
millMin = ((60 - cMin) + aMin) * 600000;
} else millMin = (aMin - cMin) * 600000;
tillAlarm = millHour + millMin;
clearInterval(alarmLoop);
alarmLoop = undefined;
alarmLoop = setTimeout(function() {
alert("alarm");
}, tillAlarm);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment