I've success set single time to send alert. But I have problem to set multiple time. How set multiple time to send alert? Example: I have to send alert at 14:05, 17: 35 and 19:12
<script type="text/javascript">
var alarmDate = new Date();
alarmDate.setHours(21);
alarmDate.setMinutes(47);
// set day, month, year, etc.
function setAlarm(){
var currentDate = new Date();
if (currentDate.getHours() == alarmDate.getHours() &&
currentDate.getMinutes() == alarmDate.getMinutes()
/* compare other fields at your convenience */ ) {
alert('Alarm triggered at ' + currentDate);
// better use something better than alert for that?
}
}
setInterval(setAlarm,1000)
</script>
Use setTimeout instead. There is no reason to call a piece of code to cehck what time it is every single second. Just wait until the time when you want to execute something.
// Set your alarm date to whatever you require
var alarmDate = new Date();
alarmDate.setSeconds(alarmDate.getSeconds() + 5);
setAlarm(alarmDate, function(){
alert('Alarm triggered at ' + new Date());
});
var alarmDate2 = new Date();
alarmDate2.setSeconds(alarmDate2.getSeconds() + 10);
setAlarm(alarmDate2, function(){
alert('Alarm triggered at ' + new Date());
});
function setAlarm(date, callback){
// How much time from now until the alarmDate
var timeUntilAlarm = date - new Date();
// Wait until the alarm date, then do stuff.
setTimeout(callback, timeUntilAlarm);
}