PDA

For continued disscussion on this topic : Cycling thru images based on date...



gzazJim
06-30-2000, 08:39 PM
Greetings fellow code-monkeys (or is that junkies...?),

I am trying to come up with a way to determine what "Shift" today is. Apparently I am completely muddled in my thinking, because I cannot figure out HOW to do this!

This is the full skinny - Here at the Fire Department most people work in "shifts" - A-Shift, B-Shift and C-Shift; each of these shifts have an indicator that displays on our home page on the appropriate day. The shift-change is at 8:00 am. For example: today is A-Shift, so the "A.gif" is displayed; at 8 am on Saturday, the shift changes to B-Shift, so the image changes to the "B.gif" image.

Right now I have a REALLY UGLY solution that involves a "shiftscript.js" which steps through an if/then/else set-up which I have hard-coded with the month/day/year. Needless to say, the file is HUGE and pretty ridiculous to update.

Here's a snippet of the code *embarrassed look* :

if (now.getMonth() == 7)
{
if (now.getDate() == 1)
{
if (now.getHours() >= 8)
{
document.shiftInd.src= shiftName[2]
}
if (now.getHours() < 8)
{
document.shiftInd.src= shiftName[1]
}
}
if (now.getDate() == 2)
{
if (now.getHours() >= 8)
{
document.shiftInd.src= shiftName[0]
}
if (now.getHours() < 8)
{
document.shiftInd.src= shiftName[2]
}
}
if (now.getDate() ==... (and so on)


(I wasn't kidding when I said it was ugly...)

Does anyone have some thoughts on how to code this in such a way that it will not require much (or any) maintenance? Again, I am rather muddled when it comes to time/date-related issues (just ask any of my ex-girlfriends...).

Thanks in advance!

Jim

Vincent Puglia
06-30-2000, 09:05 PM
Hi Jim,

I haven't thought this through, but...
How long is each shift?
You should be to determine the shift by 'mod'ding (%) or offseting from a base time.
var baseTime = new Date(2000,01,01,08,00,00);

The offsets would be:
shiftA = baseTime;
shiftB = baseTime + shiftLength;
shiftC = shiftB + shiftLength;

Then you should be able to figure out how many hours have passed since 8 am Jan 1 and put up the appropriate gif.
(you'll probably have to convert to milliseconds)
If you don't need it immediately, I'll give it a stab.

Vinny

gzazJim
06-30-2000, 09:10 PM
Vincent,

The shifts are 24-hours long so they run from 8:00:00 a.m. on Day X to 7:59:59 a.m. on Day Y. I had a hunch I would have to determine things the way you stated, but I was afraid I'd just mess things up somehow!

Any help is appreciated, and I'm not a big hurry on it - the "Uglyscript" works OK for now. Thanks for not laughing too hard!

Thanks again,

Jim

Vincent Puglia
06-30-2000, 09:20 PM
Hi Jim,

Even easier, I thought it was going to be 36 or somesuch hours. I'll see what I can come up with.

John: This is my second post -- the first got dumped to a 'cannot connect' -- I hope it was my machine / connection. If it happens again, I'll email you.

Vinny

Ryan Keough
06-30-2000, 09:38 PM
Have you checked at http://www.dynamicdrive.com or http://www.wsabstract.com?

I have seen a script that will do exactly what your looking for, but I dont remember which site it was.

Vincent Puglia
07-01-2000, 12:14 AM
Hi Jim,

Didn't test this (in fact, still writing it) :)

numOfPics = 3;

var baseDate = new Date(2000,01,01,08,00,00);
var baseTime = baseDate.getTime();
var today = new Date();
var todayTime = today.getTime();
var dayInMSecs = 1000*60*60*24;

timeDiff = baseTime - todayTime;
timeDiff /= dayInMsecs;
picNum = timeDiff % numOfPics;
switch (shiftPic)
{
case 0:
document....src = "A.gif";
break;
case 1:
document....src = "B.gif";
break;
case 2:
document....src = "C.gif";
break;
}

As I said, I haven't tested it, so run it & check against the old code. It should be good though.

Btw: if you remove these 2 lines:
var dayInMSecs = 1000*60*60*24;
timeDiff /= dayInMsecs;

The premise is for an 8 hour shift, 3 pics -- but it should also work for 6 hours and 4 pics -- or any other multiple of 24.

timeDiff /= dayInMsecs; // you may have to write this out as: timeDiff = timeDiff / dayInMsecs;

(told you i was still writing this; this is my second edit)

Vinny

[Edited by Vincent Puglia on 07-01-2000 at 03:02 PM]

gzazJim
07-03-2000, 05:08 PM
Vincent,

Thanks for taking the time to walk through all this stuff for me - it is much appreciated! I am going to mess about with things a bit today (seeing as how there's NO ONE here), and I'll leet you know how it ends up working out!

Thanks again,

Jim

gzazJim
07-11-2000, 10:22 PM
Vincent, et al...

I FINALLY got this to work. Vincent, thank you much for all your assistance - basically I cursed and sweated for a while, but based on what you had given me, I finally got this script working correctly. The script now cycles through our "Shift Indicator" images perfectly (they even change at 8 AM like they are supposed to).

Thanks again Vincent - The assistance was MUCH appreciated.

Jim

P.S. Here's the final script:

<html>
<head>
<script language="JavaScript">
<!-- hide
var shiftIndi = new Array ("images/A-Shift.gif", "images/B-Shift.gif", "images/C-Shift.gif");
function getShift()
{
if (document.images)
{
numOfPics = 3;
var baseDate = new Date(2000,01,01,08,00,00);
var baseTime = baseDate.getTime();
var today = new Date();
var todayTime = today.getTime();
var dayInMSecs = 1000*60*60*24;

timeDiff = todayTime - baseTime;
timeDiff = timeDiff / dayInMSecs;
<!-- this next line was the one that was causing me all the headaches... I needed to use Math.floor on the MOD operator... -->
picNum = Math.floor(timeDiff % numOfPics);

switch (picNum)
{
case 0:
document.shiftInd.src = shiftIndi[0];
break;
case 1:
document.shiftInd.src = shiftIndi[1];
break;
case 2:
document.shiftInd.src = shiftIndi[2];
break;
}
}
}
// end hide -->
</script>
</head>
<body onload="getShift()">
<img src="http://firewire/images/blankTEST.gif" NAME="shiftInd" Align=right alt="Shift Indicator" width="108" height="108">
</body>
</html>

Vincent Puglia
07-11-2000, 10:34 PM
Hi Jim,

:) Glad it's up and running -- was afraid I screwed it up royally and you were being discreet about it.

:( Sorry about the Math.floor() The only excuse I've got is: I wasn't thinking.

Vinny