Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Inactivity

Inactivity

mark87
01-08-2006, 12:22 AM
Right starting to experiment with Javascript again!

Got an idea from an article I read on checking for inactivity, so decided to give it a go using Javascript. It was working, but I decided to change it round a bit and now it's not. The error line points to the <body> tag line (but I'm sure that it doesn't mean the error is on that line, just that one of the functions it points to contains the error?).

Anyone see what's wrong with it? (I've taken out comments to make it easier to read, but can add them back in if needbe.)


<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.";
}

if ( Period < Seconds ) {
document.getElementById('message').innerHTML = "You are active.";
if document.getElementById('message').style.color = "#000" { document.getElementById('message').style.color = "#F00"; }
else { document.getElementById('message').style.color = "#000" }
}
}

function ResetTimer() {
Period = 0;
}
</script>

<body onLoad="Begin();" onMouseMove="ResetTimer();">

_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.

mark87
01-08-2006, 01:05 AM
Ok thanks. As mentioned it was just a little play and won't be used on a webpage so I didn't bother seperating it from the content (which I would normally do). :)


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

Hyperthreading hurts s
HP sharpens blade PC l
problem with an ImageP
Modifying a navigation
convert text to intege
Anyone knows to clear 
Automatically generate
document.getElementByI
Hi question about stor
Netscape Problems :-( 

News Archive

Month and Year drop do
navigation button/imag
doubt in window.open()
undo redo script 
Mimick "View Source" w
print Question 
No-image representatio
What's wrong with this
Urgent help in Javascr
Gurt Menu assistance 

Related stories:

What's wrong with this fadein?
Urgent help in Javascript calendar display
Gurt Menu assistance
<select onChange=" "> ??
Todo es posible...
Making multiple pop-ups?
Limit Javascript to just ONE table?

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics