Well, I did alter the code so that I could use a <script> tag to post it and post just the information I wanted instead of the picture and title all at once. I have .js files for the picture then the title so I can do different things with them - that's a different story. One of my files looks like this:
Code:
var GMToffset = -6; // Your current GMT offset, whether Standard or Daylight
var now = new Date();
now.setHours(GMToffset + now.getHours() + now.getTimezoneOffset() / 60);
var hours = now.getHours();
var mn = now.getMinutes();
hours = hours + (mn/60);
//Configure message below to your own.
if (hours>=1&&hours<=2.25) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/rabbitanddavid2.bmp"/>')
else if (hours>=3&&hours<=4.25) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/sad2.bmp"/>')
else if (hours>=6&&hours<=7) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/morning2.bmp"/>')
else if (hours>=12&&hours<=13) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/featuredshow2.bmp"/>')
else if (hours>=15&&hours<=16.50) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/passtime2.bmp"/>')
else if (hours>=18&&hours<=20) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/passtime2.bmp"/>')
else if (hours>=20&&hours<=21.50) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/passtime2.bmp"/>')
else if (hours>=21.50&&hours<=23) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/playlist2.bmp"/>')
else if (hours>=23&&hours<=0.50) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/davidshow2.bmp"/>')
else if (hours>=0.50&&hours<=0.75) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/rockiesrecap2.bmp"/>')
else //MESSAGE FOR LATE NIGHT, EARLY MORNING (12pm-4am)
document.write('<img src="/images/playlist2.bmp"/>')
This code has been working very well for the time I have been using it until it suddenly stopped working during the 11pm hour. I'm not sure why. I have been through it to make sure I don't have extra or stray punctuation such as; ' marks that might alter the way the code works and I can find no errors. It only started doing this after I changed 12am from 24 to 0. Now the 12am hour works but the 23 does not. I have also tried elliminating the 0 and doing different time frames that have been working; i.e.: 23 - 1 and the 23 hour still does not work. I'm sure it's just a simple solution that I am not seeing to my unprofessional eye.
I want to add that I just copied the lines when I wanted to add another picture for another show so pretty much all of the lines have "//MESSAGE FOR AFTERNOON" after it but that doesn't or shouldn't affect the way the code works in any way, I just never deleted these messages.
The hours value cannot be both greater than 23 and less than 0.5.
Change this line to
else if (hours>=23 || hours<=0.50)
Can you see why it would work in the 23 hour if the hours value was 24.5 (which of course can never occur)? Comparison using && requires that the second of the two numbers is greater than the first.
Although it is not a fatal error here you have assigned the same time to two conditions, e.g.
else if (hours>=21.50&&hours<=23) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/playlist2.bmp"/>')
else if (hours>=23&&hours<=0.50) //MESSAGE FOR AFTERNOON
document.write('<img src="/images/davidshow2.bmp"/>')
You should preferably assign 23 hours to one or the other.
You have omitted the braces { } around your if statements. That is allowable if and only if only one line of code follows. It is very poor practice as changes or additions to the code may well foobar it.
Oh haha, rookie mistake, right? lol
It makes sense when it's explained. Otherwise i'm on my own scratching my head and changing the code around until NOTHING works.
That does make sense that when using && 0.50 would have to be greater than 23 - it's obviously not. So, for 23 on two conditions, will that cause it to not work correctly? I thought it would be ok because I was thinking about it and on one condition it is using less than 23 <23, and the other greater than 23 >23, I thought those were two different things. On a script like this just including 23 on two scripts will mess it up?
As far as brackets go, do I need a set before the "else" and at the end of the document line of each condition?
I know you said it's not a fatal error, so does that mean it will not cause the script to not work? Will the pictures still change at or a minute after the 23 hour?
Thanks again!
You probably don't want to modify your code but, if it were me, I think I would prefer to keep the hours and minutes separate:
Code:
var hours = now.getHours();
var mn = now.getMinutes();
// hours = hours + (mn/60); - remove this
if (hours>=1&&hours<=2.25) //MESSAGE FOR AFTERNOON
if (hours==1 || (hours==2 && mn<=15)) // alternative version
// believe this is right?
This avoids any concerns about rounding and allows you to check for equality. But, as I say, you probably don't want to change it now.
Ok. That's pretty neat. So, the thing that confuses me about that alternative version is all the parenthesis ( ), what would it look like if I had minutes on both start and end hours?
if ((hours==20 && mn=<15) || (hours==20 && mn<=30))
document.write('<img src="/images/playlist2.bmp"/>')
???? - the one above doesn't seem to work lol I'm sure I screwed that all up, too many parenthesis maybe...
Ok. That's pretty neat. So, the thing that confuses me about that alternative version is all the parenthesis ( ), what would it look like if I had minutes on both start and end hours?
if ((hours==20 && mn=<15) || (hours==20 && mn<=30))
document.write('<img src="/images/playlist2.bmp"/>')
???? - the one above doesn't seem to work lol I'm sure I screwed that all up, too many parenthesis maybe...
you will need to be very careful about rounding errors.
(Looks like you'll be up til midnight )
Why? Explain please? My code turns minutes into fractions of an hour to avoid the malarky you propose.
And extreme accuracy to the second is not required for the display of a message at a certain time.
.3334 of an hour is 20.004 minutes. The error is 0.24 seconds. Bearing in mind that the time is taken from the user's clock which could be seconds or minutes out, there is little point in attempting any additional accuracy.
In fact the times are at 15, 30 or 45 minutes where no rounding takes place.
Rounding errors are indeed important is some situations. But this is not one of them.
I shall retain my preference for distinguishing between hours and minutes.
That is perfectly OK. As we have said many times before in this forum, there are several different ways to skin a cat. If you want to do something the hard way that is fine by me.
You are quite free to get the value of a selected option with
document.formname.selectlistname.options[document.formname.selectlistname.selectedIndex].value
if you want.
Once again - bearing in mind that the time is taken from the user's clock which could be seconds or minutes out, there is little point in attempting any additional accuracy in client-side timers in any situation.