_Aerospace_Eng_
01-08-2006, 01:00 AM
Parentheses anyone?
if (document.getElementById('message').style.color == "#000") { document.getElementById('message').style.color = "#F00"; }
Since you are comparing values you should use the double equal sign. I think this method is a bit more efficient. Less lines of code, same job is done.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
var Interval, Period, Seconds;
function Begin() {
Seconds = 15;
ResetTimer();
Interval = window.setInterval("Check()",1000);
}
function Check() {
Period++;
if ( Period >= Seconds ) {
document.getElementById('message').innerHTML = "You were inactive for " + Period + " seconds.";
document.getElementById('message').style.color="#F00";
}
if ( Period < Seconds ) {
document.getElementById('message').innerHTML = "You are active.";
document.getElementById('message').style.color="#008000";
}
//if (document.getElementById('message').style.color == "#008000") {
// document.getElementById('message').style.color = "#F00";
//}
//else {
// document.getElementById('message').style.color = "#000"
//}
}
function ResetTimer() {
Period = 0;
}
window.onload=Begin;
window.onmousemove=ResetTimer;
//-->
</script>
</head>
<body>
<div id="message"></div>
</body>
</html>
IMO you should keep function separated from content meaning onload and onmousemove should be handled by the JS itself rather than the onload and onmousemove attributes. I commented out lines that aren't needed because the colors are taken care of by the other if statements.
if (document.getElementById('message').style.color == "#000") { document.getElementById('message').style.color = "#F00"; }
Since you are comparing values you should use the double equal sign. I think this method is a bit more efficient. Less lines of code, same job is done.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
var Interval, Period, Seconds;
function Begin() {
Seconds = 15;
ResetTimer();
Interval = window.setInterval("Check()",1000);
}
function Check() {
Period++;
if ( Period >= Seconds ) {
document.getElementById('message').innerHTML = "You were inactive for " + Period + " seconds.";
document.getElementById('message').style.color="#F00";
}
if ( Period < Seconds ) {
document.getElementById('message').innerHTML = "You are active.";
document.getElementById('message').style.color="#008000";
}
//if (document.getElementById('message').style.color == "#008000") {
// document.getElementById('message').style.color = "#F00";
//}
//else {
// document.getElementById('message').style.color = "#000"
//}
}
function ResetTimer() {
Period = 0;
}
window.onload=Begin;
window.onmousemove=ResetTimer;
//-->
</script>
</head>
<body>
<div id="message"></div>
</body>
</html>
IMO you should keep function separated from content meaning onload and onmousemove should be handled by the JS itself rather than the onload and onmousemove attributes. I commented out lines that aren't needed because the colors are taken care of by the other if statements.
