Alarm Clock

[Total: 0    Average: 0/5]

This shows how to have a simple alarm clock.

Body Code:

<form name=exf1>
Alarm Hour:<input type=text name=h value="?"><BR>
Alarm Min:<input type=text name=m value="?"><BR>
Alarm Second:<input type=text name=s value="?"><BR>
<input type=button name=b onclick=setAlarm() value="Set Alarm"><BR>
<input type=button name=r onclick=clearAlarm() value="Turn Alarm Off"><BR>
Current Hour:<input type=text name=ch value="?"><BR>
Current Minute:<input type=text name=cm value="?"><BR>
Current Second:<input type=text name=cs value="?"><BR>
</form>
<script>
var alarmTimer = null;
var alarmSet;
function setAlarm()   { alarmSet = true;  }
function clearAlarm() { alarmSet = false; }
function initAlarm() {
  if (alarmTimer!=null)clearInterval(alarmTimer);
  var nowTime = new Date();
  clearAlarm();
  document.exf1.h.value = nowTime.getHours();
  document.exf1.m.value = nowTime.getMinutes();
  document.exf1.s.value = nowTime.getSeconds();
  alarmTimer=setInterval("countTime()",1000);
}
function matchH() { return (document.exf1.ch.value == document.exf1.h.value); }
function matchM() { return (document.exf1.cm.value == document.exf1.m.value); }
function matchS() { return (document.exf1.cs.value == document.exf1.s.value); }
function countTime() {
  var nowTime = new Date();
  document.exf1.ch.value = nowTime.getHours();
  document.exf1.cm.value = nowTime.getMinutes();
  document.exf1.cs.value = nowTime.getSeconds();
  if (matchH() && matchM() && matchS()) {
    alert("ALARM!");
  }
}
onload=initAlarm;
</script>

Times Viewed: 31

Leave a Reply

Your email address will not be published. Required fields are marked *