The source code below is a digital alarm clock, but I have a question.
In the javascript of the clock, "check time" says "flg=-1", but "flg=1" and "flg=0" in "function changeFlg(){}", so if this number (-1) is "zero and non-one", all computers will function normally.
Is it acceptable to change numbers arbitrarily like this?
Also, no sound is set.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=shift_jis">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<bgsound id="bgm" src="222.mid"loop="-1">
<TITLE> Digital alarm sound</TITLE>
<!--- Clock JavaScript--->
<SCRIPT type="text/javascript">
<!--
// Initialize the alarm flag.
varflg = 0;
// Time update & alarm check function
function timeCheck(){
// Get the time.
Now = new Date();
Hour=Now.getHours();
Min = Now.getMinutes();
Sec = Now.getSeconds();
// If "minutes" and "seconds" are single digits, mark them as two digits with a zero at the beginning.
if(Hour<=9){
Hour="\u0020" + Hour;
}
if(Min<=9){
Min = "0" + Min;
}
if(Sec<=9){
Sec = "0" + Sec;
}
// Displays the time.
document.sampleForm.dspTime.value=Hour+":"+Min+":"+Sec;
// Check the time.
if((flg==1)&(document.sampleForm.alarmH.value==Hour)&(document.sampleForm.alarmM.value==Min)){
document.getElementById('bgCol') .value = "333.wav", selectBgm( document.getElementById('bgCol')),
a number other than flg=-1;//0 and 1
}
}
// Alarm flag change function
function changeFlg(){
if(flg==0){
// Alarm not set,
document.sampleForm.setAlarm.value="alarmOFF";
document.getElementById("bgCol").value="";
selectBgm( document.getElementById('bgCol'));
flg = 1;
} else {
// When the alarm is set,
document.sampleForm.setAlarm.value="alarm ON";
document.getElementById("bgms").reset();
selectBgm( document.getElementById('bgCol'));
flg = 0;
}
}
// Set the following updates:
setInterval (timeCheck, 100);
window.onload=timeCheck;
// -- >
</SCRIPT>
<script type="text/javascript">
<!--
function selectBgm(e){
var selectedIndex=e.selectedIndex;
document.getElementById("bgCol").style.background=e [selectedIndex].style.backgroundColor;
bgm.src=e [selectedIndex].value;
document.getElementById("bgCol").value=e[selectedIndex].value;
}
// -- >
</script>
</head>
<BODY color="gold" bgcolor="black">
<form id="bgms" style="text-align:right">
<SELECT id="bgCol" onchange="selectBgm(this);">
<OPTION value="">Music selection (stop)</OPTION>
<OPTION value="222.mid" selected> Classic</OPTION>
<OPTION value="333.wav">Alarm tone</OPTION>
</SELECT>
</form>
<FORM NAME="sampleForm" style="text-align:center">
<INPUT type="text" size=7NAME="dspTime">
<br>br>
<br>br>
<div>
<INPUT type="text" name="alarmH" size=2 value="00">
<INPUT type="text" name="alarmM" size=2 value="00">
<INPUT type="button" id="setAlarm" name="setAlarm" value="alarm ON" onClick="changeFlg();">
</div>
</FORM>
</BODY>
</HTML>
By design, it is desirable not to approve.
For the above clock, the variable "flg" is
flg = 0 ... Alarm not set (initial value)
flg=1...alarm configuration state
It can be read that it exists to keep the alarm ON/OFF.
If you set the variable "flg" to -1,
Turn off the alarm after it has occurred to prevent it from ringing again and again.
It can be read as if it were for the .
If so, it is desirable to set the alarm to "flg=0" without setting according to the above setting.
Configuring an out-of-design value of "flg=-1" causes confusion.
Confusion is the source of the defect.
This source code looks like a "flg=1 and other" design.
You just use "0" as the "other" value, and anything but "1".
It is not desirable to use floating values that are undefined by design.
*Why is the name of the audio file to be played bgCol?
© 2024 OneMinuteCode. All rights reserved.