Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1141 How can I setup a clock with defined alarm times which trigger another event?

You are here: irt.org | FAQ | JavaScript | Date | Q1141 [ previous next ]

Try:

<script language="JavaScript"><!--
function makeAlarm() {
    for (i = 0; i<makeAlarm.arguments.length; i++) {
        time = makeAlarm.arguments[i];
        this[i] = zero + (Math.floor(time)*60*60*1000) + (time - Math.floor(time))*100*60*1000;
    }
    alarms = i;
}

var now = new Date();
var zero = (new Date(now.getYear(),now.getMonth(),now.getDate(),0,0,0)).getTime();
var myAlarms = new makeAlarm(0.00,8.59,11.59,23.59);

function checkAlarm() {
    now = new Date();

    for (var i=0;i<alarms;i++) {
        diff = myAlarms[i]-now.getTime();
        if (diff > 0 && diff < 1000*60)
            alert(now);
    }

    setTimeout('checkAlarm()',1000*60); // check every minute
}

checkAlarm();
//--></script>

©2018 Martin Webb