Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-16-2011, 06:22 AM   PM User | #16
rgy
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
rgy is an unknown quantity at this point
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.
rgy is offline   Reply With Quote
Old 04-16-2011, 06:27 AM   PM User | #17
rgy
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
rgy is an unknown quantity at this point
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.
rgy is offline   Reply With Quote
Old 04-16-2011, 07:34 AM   PM User | #18
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
else if (hours>=23 && hours<=0.50)

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.

Last edited by Philip M; 04-16-2011 at 07:46 AM..
Philip M is offline   Reply With Quote
Old 04-16-2011, 09:55 PM   PM User | #19
rgy
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
rgy is an unknown quantity at this point
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!

Last edited by rgy; 04-17-2011 at 05:16 AM..
rgy is offline   Reply With Quote
Old 04-16-2011, 10:20 PM   PM User | #20
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Hint: Because you're using the expression
Code:
hours = hours + (mn/60);
you will need to be very careful about rounding errors.

(Looks like you'll be up til midnight )
AndrewGSW is offline   Reply With Quote
Old 04-16-2011, 10:51 PM   PM User | #21
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
AndrewGSW is offline   Reply With Quote
Old 04-17-2011, 03:12 AM   PM User | #22
rgy
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
rgy is an unknown quantity at this point
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...
rgy is offline   Reply With Quote
Old 04-17-2011, 05:07 AM   PM User | #23
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,762
Thanks: 29
Thanked 452 Times in 446 Posts
jmrker will become famous soon enough
Arrow

Quote:
Originally Posted by rgy View Post
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...
CAREFULLY re-read his suggestion of post #21.
jmrker is online now   Reply With Quote
Old 04-17-2011, 10:38 AM   PM User | #24
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AndrewGSW View Post
Hint: Because you're using the expression
Code:
hours = hours + (mn/60);
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.
Philip M is offline   Reply With Quote
Old 04-17-2011, 02:26 PM   PM User | #25
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Well extreme accuracy is probably not such a concern with this example, although awareness of rounding errors is important.

I just prefer the malarky of separating the hours and minutes. I know that 'mn == 20' means it's twenty past, rather than

Code:
(hours - parseInt(hours)) >= 0.3 possibly && 
(hours - parseInt(hours)) < 0.33333333333333334 possibly
AndrewGSW is offline   Reply With Quote
Old 04-17-2011, 05:58 PM   PM User | #26
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
.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.
Philip M is offline   Reply With Quote
Old 04-17-2011, 06:50 PM   PM User | #27
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I wondered when you might come back to me

I did not know that ".3334 of an hour is 20.004 minutes".

I shall retain my preference for distinguishing between hours and minutes.
AndrewGSW is offline   Reply With Quote
Old 04-18-2011, 08:57 AM   PM User | #28
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AndrewGSW View Post
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.
Philip M is offline   Reply With Quote
Old 04-18-2011, 01:00 PM   PM User | #29
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Okay.
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Tags
lee, message, mills, schedule, time of day

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:55 PM.


Advertisement
Log in to turn off these ads.