Forum Moderators: open

Message Too Old, No Replies

timing events

I think my math is off

         

too much information

2:01 pm on Sep 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script that I put together to submit a form at different times of the day. Specifically: onload, 12:00, 1:00, 5:00 but I'm having trouble getting the script to refresh the page and start over at 8:00am the next morning so I don't have to load the page manually.

I thought my math was correct because it works well throughout the day but it submitted the form at 11:30 ish last night and submitted the value that is supposed to trigger at 5:00.

I'm no JavaScript expert but here is what I have:

<!--
// This script times each portion of the day
// and sets the appropriate value in the IO form
//
// rand() generates a random number between 0 and the number provided
//
// The form is triggered by autoIO() for the following
// values:
// 0 = In
// 1 = Out
// 2 = Lunch
//
function dayGo()
{
Stamp = new Date();
var Hours;
var Mins;
Hours = Stamp.getHours();
Mins = Stamp.getMinutes();

// Set time using the following method
minConvert = 60000; //60 seconds
hourConvert = 60 * minConvert;

// Set time for lunch 12:00
hrLeft = 12 - Hours - 1;
minLeft = 60 - Mins;
if (hrLeft > 0){
lengthOfTime = (hrLeft * hourConvert) + (minLeft * minConvert) + (rand(5) * minConvert);
setTimeout('autoIO(2)', lengthOfTime);
}

// Set time back from lunch 1:00
hrLeft = 13 - Hours - 1;
minLeft = 60 - Mins;
if (hrLeft > 0){
lengthOfTime = (hrLeft * hourConvert) + (minLeft * minConvert) + (rand(5) * minConvert);
setTimeout('autoIO(0)', lengthOfTime);
}

// Set time to go home 5:00
hrLeft = 17 - Hours - 1;
minLeft = 60 - Mins;
if (hrLeft > 0){
lengthOfTime = (hrLeft * hourConvert) + (minLeft * minConvert) + (rand(5) * minConvert);
setTimeout('autoIO(1)', lengthOfTime);
}

// Set time to start over
hrLeft = 14;
minLeft = 60 - Mins;
if (hrLeft > 0){
lengthOfTime = (hrLeft * hourConvert) + (minLeft * minConvert) + (rand(5) * minConvert);
setTimeout('location.reload()', lengthOfTime);
}
}
//-->

The part that seems to not work is the part that sets the time to start over. I've tried making this dynamic (based on what time the script is loaded) and this version is a fixed time, but I can't seem to get either to work.

Any Ideas?

Bernard Marx

9:46 pm on Sep 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about letting the CPU do the thinking ..?


TIMED LOOP : 55 sec // < 1 min
IF ( time = 12:00 OR 1:00 OR 5:00 )
postForm()
ENDIF
ENDLOOP

..and an onload function too.

Not the most efficient, but possibly more efficient, because there's only one timer running (timers take juice). Running this little script very 55 sec isn't going to bother any chip these days.

too much information

12:44 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for that tip!

I am proficient in PHP and ASP, but I just do not get JavaScript.

I had no idea how to check the time. I'll give that a try.

too much information

7:44 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, here's what I have:

<!--
// This script times each portion of the day
// and sets the appropriate value in the IO form
var thecount = 0;
function dayGo2()
{
time = "" + now.getHours() + ":" + now.getMinutes();
//Set time in
if ( time = "8:00" ) {autoIO(0)};
//Set lunch time
if ( time = "12:00" ) {autoIO(2)};
//Set lunch over
if ( time = "13:00" ) {autoIO(0)};
//Set time out
if ( time = "17:00" ) {autoIO(1)};
document.ioTimer.loopCount.value = thecount;
thecount += 1;
timeUpdate = setTimeout('dayGo2()',55000);
};
//-->

I added a field to my form so that I could watch the loop execute but it doesn't seem to be working.

Like I said, I just don't "get" JavaScript. It seems like it should be working fine so obviously I'm missing something.